random_shuffle uses rand by default so you can seed it with srand.
Type: Posts; User: lmov
random_shuffle uses rand by default so you can seed it with srand.
Hello,
You can make an array of elements 1...n and then shuffle them to get a random premutation:
#include <vector>
#include <algorithm>
#include <iostream>
int main()
{
const int N =...
Number / Number is 1 unless Number is 0 in which case it's undefined.
To get the square root of a number you can use the sqrt function in <cmath>
#include <cmath>
#include <iostream>
using...
operator>> reads only up to the next whitespace character (space, tab, newline). You can use std::getline to read up to whatever delimiter you want:
#include <iostream>
#include <string>
int...
Hello,
The difference between '\n' and endl is that endl also flushes the stream. So:
cout << endl;Is the same as:
cout << '\n'; cout.flush();'\n' will give you just a newline.
You should use...
Hello,
Quite wrong. This code will call the default constructor for name then use the assignment operator to assign x to name. It is always better to initialize members in the initializer list. It...
It's even easier than that:
#include <string>
#include <algorithm>
#include <iostream>
using namespace std;
int main()
{
string s = "Hello, world!";
If you know the index then you can find an iterator to it by doing vec.begin() + index. So the erase would look like:
vec.erase(vec.begin() + index);
Hello,
Copying a string in C++ is very easy:
#include <iostream>
#include <string>
using namespace std;
int main()
{
#include <cstdlib>
#include <iostream>
int main()
{
std::cout << "Stupid int main thingy!! I'm sick of it!!";
std::system("pause");
}C++ puts everything in namespace std (except...
The default constructor generated automatically by the constructor will do the same any other constructor does, namely - make sure the object is in a usable state (that is, initialize anything...
The first one is the binary operator+ which gets called when you place it between two operands:
v1 + v2; // same as: v1.operator+(v2);The second one is the unary operator+ which gets called if you...
While there is no GetStockBrush function, there is the GetStockBrush macro in <windowsx.h>. See question 4.2 at http://winprog.org/faq/.
I did not say that casts should never be used, I said that...
A cast is never nice and should be avoided. The segment could be written like this:
#include <windowsx.h>
// ...
windowClass.hbrBackground = GetStockBrush(WHITE_BRUSH));
#include <string>
#include <algorithm>
#include <cctype>
using namespace std;
string remove_whitespace(string s)
{
s.erase(remove_if(s.begin(), s.end(), &isspace), s.end());
return...
#include <iostream>
struct A
{
void f()
{
std::cout << "A::f\n";
}
};
std::strlen(/*whatever*/); or ::strlen(/*whatever*/); depending on how compliant your compiler is. Since you're including <string.h> it will probably be the latter. Try to make a habit out of using...
Not quite. std::string defines the member function swap, which has constant-time complexity. So:
std::string array[] = { "string1", "string2", "string3", "string4" };
array[1].swap(array[2]); //...
When you want to find the size of a std::string, use size():
#include <string>
#include <iostream>
using namespace std;
int main()
{
string s = "Hello, world!";
cout << s.size() <<...
When you get an error from a WinAPI function, always check GetLastError to see what went wrong:
#include <windows.h>
const char* ErrorMessage(DWORD error)
{
static char buffer[255];
...
I believe Windows releases the memory your application has allocated when it closes. However, other resources may not be released so it is a good idea to always explicitly release what you have...
Turn off the monitor.
Seriously, read the FAQ.
Thanks to the wonderful world of C++ parsing, in typedef map<int, string, less<int>> C; the >> is a single token. Put a space between the greater than characters and everything should be fine:...
Don't confuse an array with a pointer. See: http://www.eskimo.com/~scs/C-faq/q6.2.html and http://www.eskimo.com/~scs/C-faq/q6.18.html
Not in the way you would expect.