Search:

Type: Posts; User: lmov

Page 1 of 5 1 2 3 4

Search: Search took 0.01 seconds.

  1. random_shuffle uses rand by default so you can...

    random_shuffle uses rand by default so you can seed it with srand.
  2. Hello, You can make an array of elements 1...n...

    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 =...
  3. Thread: square root

    by lmov
    Replies
    8
    Views
    2,733

    Number / Number is 1 unless Number is 0 in which...

    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...
  4. Replies
    1
    Views
    871

    operator>> reads only up to the next whitespace...

    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...
  5. Replies
    7
    Views
    2,217

    Hello, The difference between '\n' and endl is...

    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...
  6. Thread: What is this?

    by lmov
    Replies
    6
    Views
    1,185

    Hello, Quite wrong. This code will call the...

    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...
  7. Replies
    9
    Views
    1,652

    It's even easier than that: #include ...

    It's even easier than that:
    #include <string>
    #include <algorithm>
    #include <iostream>

    using namespace std;

    int main()
    {
    string s = "Hello, world!";
  8. Thread: vector::erase!

    by lmov
    Replies
    3
    Views
    1,585

    If you know the index then you can find an...

    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);
  9. Thread: how to copy

    by lmov
    Replies
    5
    Views
    1,002

    Hello, Copying a string in C++ is very easy:...

    Hello,

    Copying a string in C++ is very easy:
    #include <iostream>
    #include <string>

    using namespace std;

    int main()
    {
  10. Replies
    13
    Views
    1,469

    #include #include int...

    #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...
  11. Replies
    2
    Views
    1,113

    The default constructor generated automatically...

    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...
  12. Replies
    5
    Views
    1,001

    The first one is the binary operator+ which gets...

    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...
  13. Replies
    12
    Views
    3,334

    While there is no GetStockBrush function, there...

    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...
  14. Replies
    12
    Views
    3,334

    A cast is never nice and should be avoided. The...

    A cast is never nice and should be avoided. The segment could be written like this:
    #include <windowsx.h>

    // ...

    windowClass.hbrBackground = GetStockBrush(WHITE_BRUSH));
  15. Thread: String

    by lmov
    Replies
    2
    Views
    875

    #include #include #include...

    #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...
  16. Replies
    6
    Views
    2,142

    #include struct A { void f()...

    #include <iostream>

    struct A
    {
    void f()
    {
    std::cout << "A::f\n";
    }
    };
  17. Replies
    5
    Views
    1,175

    std::strlen(/*whatever*/); or...

    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...
  18. Replies
    3
    Views
    6,570

    Not quite. std::string defines the member...

    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]); //...
  19. Replies
    13
    Views
    1,447

    When you want to find the size of a std::string,...

    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() <<...
  20. Replies
    13
    Views
    3,043

    When you get an error from a WinAPI function,...

    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];
    ...
  21. Replies
    4
    Views
    3,887

    I believe Windows releases the memory your...

    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...
  22. Replies
    10
    Views
    1,072

    Turn off the monitor. Seriously, read the FAQ.

    Turn off the monitor.

    Seriously, read the FAQ.
  23. Thread: STL Map

    by lmov
    Replies
    1
    Views
    1,006

    Thanks to the wonderful world of C++ parsing, in...

    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:...
  24. Replies
    2
    Views
    923

    Don't confuse an array with a pointer. See:...

    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
  25. Thread: get code out exe

    by lmov
    Replies
    2
    Views
    1,984

    Not in the way you would expect.

    Not in the way you would expect.
Results 1 to 25 of 101
Page 1 of 5 1 2 3 4