C++ programming and STL facts

  1. Assign value by a pair of {} to a container

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // Lots of programmers write code 
    // like this one:
    pair<int, int> p = make_pair(3, 4);
      
    // while you can just do this : 
    pair<int, int> p = { 3, 4 };
      
    // even a more complex pair
    pair<int, <char, int> > p = { 3, { 'a', 10 } };

    chevron_right

    
    

  2. As we are familiar with the pair there is also one thing known as tuples.

    // A tuple can hold elements of different types
    tuple t = {3, 4, 5, 'a'};
  3. Get rid of those includes!

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // Simply use below, we do not need to 
    // include any other library such as 
    // iostream, vector, string, cmath and so on.
    #include <bits/stdc++.h>

    chevron_right

    
    

  4. Hidden function (not really hidden but not used often)

    __gcd(value1, value2)
    You don’t need to code Euclidean Algorithm for a gcd function, from now on we can use. This function returns gcd of two numbers.
    e.g. __gcd(18, 45) = 9.

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    #include <bits/stdc++.h>
    using namespace std;
    int main()
    {
        int r = __gcd(10, 15);
        cout << r;
    }

    chevron_right

    
    

    Output:

    5
    

    Note that this works only in GCC.

  5. Convert integer to string directly in c++ using direct command
    to_string();

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    #include <bits/stdc++.h>
    using namespace std;
    int main()
    {
        int a = 2665;
        string t = to_string(a);
    }

    chevron_right

    
    

    Output:

    
    
  6. Convert from string to integer
    stoi(t);

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    #include <bits/stdc++.h>
    using namespace std;
    int main()
    {
        string a = "2665";
        int t = stoi(a);
    }

    chevron_right

    
    

    Output:

    
    
  7. A set always keeps elements in ascending order:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    set s;
      
    // For descending order
    set <int, greater> s;

    chevron_right

    
    

  8. Every variable declared outside of functions are static and have the default value of 0

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    #include <bits/stdc++.h>
    using namespace std;
      
    int a[5];
    int main()
    {
        // Values in a[] are 0
        for (int i = 0; i < 5; i++)
            cout << a[i] << " ";
      
        // Values in b[] are garbage
        cout << endl;
        int b[5];
        for (int i = 0; i < 5; i++)
            cout << b[i] << " ";
    }

    chevron_right

    
    

    Output:

    0 0 0 0 0 
    4196880 0 4196368 0 -731594144
    
  9. If you declare an array inside a function the value of its elements are garbage, but if you write TYPE a[n] = { }; all elements will be zero by default.

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    #include <bits/stdc++.h>
    using namespace std;
      
    int main()
    {
        // Values in a[] are 0
        int a[5] = {};
        for (int i = 0; i < 5; i++)
            cout << a[i] << " ";
      
        // Values in b[] are garbage
        cout << endl;
        int b[5];
        for (int i = 0; i < 5; i++)
            cout << b[i] << " ";
    }

    chevron_right

    
    

    Output:

    0 0 0 0 0 
    4196896 0 4196368 0 354948080
    
  10. Number of set bits in binary representation of a number x is
    __builtin_popcountll(x)

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    #include <bits/stdc++.h>
    using namespace std;
      
    int main()
    {
        int x = 5;
        cout << __builtin_popcountll(x);
    }

    chevron_right

    
    

    Output:

    2
    

    Note : This works only in GCC.

  11. memset sets the byte values (or unsigned char) of a block of memory. We can use memset to initialize all elements of an integer array to 0 or -1 but not the other values

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    #include <bits/stdc++.h>
    using namespace std;
      
    int main()
    {
        int a[5];
      
        // all elements of A are zero
        memset(a, 0, sizeof(a));
        for (int i = 0; i < 5; i++)
            cout << a[i] << " ";
        cout << endl;
      
        // all elements of A are -1
        memset(a, -1, sizeof(a));
        for (int i = 0; i < 5; i++)
            cout << a[i] << " ";
        cout << endl;
      
        // Would not work
        memset(a, 5, sizeof(a)); // WRONG
        for (int i = 0; i < 5; i++)
            cout << a[i] << " ";
    }

    chevron_right

    
    

    Output:

    0 0 0 0 0 
    -1 -1 -1 -1 -1 
    84215045 84215045 84215045 84215045 84215045
    


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :
Practice Tags :


1


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.