This question already has answers here:
Sleep for milliseconds
(20 answers)
Closed 1 year ago.
In python the time.sleep() is for count down time before execute next code:
import time
time.sleep(5)
print("something")
So what is the equivalent to that in C++. Sorry for asking this simple question but I am new to C++
The "sleep_for" allows you to give the time you want to delay, and "sleep_until" allows you to delay until a exact time.
#include
#include
int main() {
using namespace std::this_thread; // sleep_for, sleep_until
using namespace std::chrono; // nanoseconds, system_clock, seconds
sleep_for(nanoseconds(10));
sleep_until(system_clock::now() + seconds(1));
}
Related
This question already has answers here:
How to print function pointers with cout?
(7 answers)
Why am I always getting output as 1 when printing a function?
(2 answers)
Closed 12 months ago.
So, I just started with C++ and was doing random things with a snippet of code I found on the net. The code is a simple use of defining namespace. After my changes in the code, it looked like,
#include <iostream>
using namespace std;
namespace ns1 { int value() {return 5;}}
namespace ns2 { int value() {return -5;}}
int main() {
cout << ns1::value<< '\n'; //5 will be displayed
cout << ns2::value<< '\n'; // -5 will be displayed
}
Now, i know that i have called the wrong function and it should be ns1::value() instead of ns1::value , but my question is why is the code still working? And why is it giving an output of 1?
This question already has answers here:
How to alloc a executable memory buffer?
(5 answers)
__asm__ gcc call to a memory address
(1 answer)
Closed 3 years ago.
When programming in Win32, one can execute data as follows:
#include <iostream>
using namespace std;
typedef double(*func)(void);
int main()
{
// The sample program to display the PI number
uint8_t code[] = { 0xD9,0xEB,0xC3 }; // fldpi; ret
uint8_t* p = code;
func f = reinterpret_cast<func>(p);
cout << f() << endl;
return 0;
}
This trick allows to speed up some numeric calculations when the expression to calculate is initially unknown.
The only thing you need to do this in Win32 is to disable data execution protection by using the /NXCOMPAT:NO Visual Studio Option.
But how to do this in Win64? Is this even possible?
This question already has answers here:
Loop every 10 second
(7 answers)
Closed 7 years ago.
I'm trying to write a loop that runs once every x milliseconds, is there a way to do something like this effectively in c++?
Hope someone could help me with an example how I would go about writing a loop like this
One and the simplest approach would be using windows.h library's Sleep() function.
#include "windows.h"
...
while(1)
{
for(...) {} // your loop
Sleep(miliseconds);
if(something) { break; } // to prevent infinite looping.
}
...
A better solution would be using std::this_thread::sleep_for() from < thread > header.
more documentation and examples here.
This question already has answers here:
Define bitset size at initialization?
(7 answers)
Closed 5 years ago.
Now I've this code:-
The code is about taking in an integer and providing its binary form in the given number of bits.
#include <iostream>
#include <bitset>
using namespace std;
int main(){
//creating instance using bitset (6 bit). here you can specify the length such as 8,16,32,64...
int n=5;
bitset< 6 > btFlaged;
//assigning integer value to instance
btFlaged = 7;
//print bit string in the string
for(int i=btFlaged.size()-1;i>-1;i--)
{
cout <<btFlaged.test(i);
}
}
How do I use an integer(E.g. n) in the place of '6' so that a value entered at run-time can be used in the code?
I've done some research on the net and I know that bitset needs a value at compile time and so instead of bitset, I should use vector bool but I don't know how I should incorporate that in the program?
If any of you guys can tell me how to use vector or if you have an altogether different theory on how to the the task done, please do share.
Also I cant use boost:dynamic_bitset as the code is going to be judged by an online judge which may not have the seperate header file.
a std::bitset's size must be set at compile time as it is a template parameter. If you need a dynamic bitset you can look at boost:dynamic_bitset
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
Hi i am kind of new in c++. I was trying to optimize my code. My code includes two for loops with a if block inside the second loop. first loop will iterate for 10^14 times and inner for loop will iterate for 10^4 times. My code is as folows
#include<iostream>
using namespace std;
#include<conio.h>
#include<stdlib.h>
#include<time.h>
#include<dos.h>
#include<stdio.h>
#include<fstream>
using namespace std;
#include<math.h>
#include<thread>
using namespace std;
signed long long run,i,j;
int main()
{
run=0;
for (i=0;i<100000000000000;i++)
{
for (j=0;j<10000;j++)
{
run=run+1;
}
}
cout<<run<<"\n";
}
time it is takling to complete is around 1 day. So I was using thread in my code to make it first. But it is showing to include -std=c++0x. So where to include this?
Is there anyone who would like to help me out?
I'd optimise it as follows, but good compilers might do that anyway:
#include<iostream>
signed long long run,i,j;
int main()
{
i = 100000000000000;
j = 10000;
run = i * j;
std::cout << run << '\n';
}
-std=c++0x is a compiling option, and it tells the compiler you are using C++11 standard.
You might want to take a look at a link like this one http://www.cs.cf.ac.uk/Dave/C/node3.html regarding compiling.