HitmanBBa's blog

By HitmanBBa, history, 9 years ago, In English

Hi everyone.

I think we can spread the science over Codeforces community, by sharing our knowledge and experiences in our blog and in that way we learn and let others learn also :D.

I will start with something simple and I hope it will help you Codeforces members :D.

Did you know that [C++] got StringTokenizer like [Java], it's implemented as a function in STL <cstring> called strtok(char * str, char * delimiters) for more information.

  • Vote: I like it
  • +38
  • Vote: I do not like it

| Write comment?
»
9 years ago, # |
  Vote: I like it +4 Vote: I do not like it

Thanks HitmanBBa for this idea, I think it's worth trying :).

  • »
    »
    9 years ago, # ^ |
    Rev. 2   Vote: I like it +9 Vote: I do not like it

    I just learnt this, I hope it will help you :)

    for_each

    #include <iostream>
    #include <algorithm>
    #include <vector>
    
    using namespace std;
    
    void print_num( int const &a )
    {
         cout << a << endl;
    }
    
    int main( )
    {
        vector < int > v;
        int i;
        for( i = 0; i < 10; i++ ) 
             v.push_back( i );
        for_each( v.begin(), v.end(), print_num ); // not just printing, you can do anything.
        return 0;
    }
    
    
    • »
      »
      »
      9 years ago, # ^ |
      Rev. 2   Vote: I like it +8 Vote: I do not like it
      1. It is iostream, not iosream.

      2.Better with c++11(Compile it with -std=c++11,submit it with G++11 4.9.2)

      #include <iostream>
      #include <vector>
      using namespace std;
      int main()
      {
          vector<int> v;
          int i;
          for(i=0;i<10;i++)
               v.push_back(i);
          for(auto& it : v){
              it++;   //modify the container,I don't know why did I write it = it+1 in Rev.1
              cout << it << endl;
          }
          return 0;
      }
      
      
      • »
        »
        »
        »
        9 years ago, # ^ |
        Rev. 2   Vote: I like it 0 Vote: I do not like it

        Sorry for such a stupid mistake '^_^

        P.S: for_each work on both c++11 and c++98, and I think ACM judges using c++98.

        • »
          »
          »
          »
          »
          9 years ago, # ^ |
            Vote: I like it +7 Vote: I do not like it

          Both in my regional and the WF we had C++11

    • »
      »
      »
      9 years ago, # ^ |
        Vote: I like it 0 Vote: I do not like it

      or you can simply do this:

      copy(v.begin(),v.end(),ostream_iterator(cout," "));

»
9 years ago, # |
Rev. 4   Vote: I like it +8 Vote: I do not like it

C++ container compare operator overloads.

#include <iostream>
#include <vector>
#include <deque>
using namespace std;
int main(){
    vector<int> v1={2,3,5,9},v2={2,3,5,9},v3={2,3,5,8};
    deque<int> d1={2,3,5,9},d2={2,3,5,9},d3={2,3,5,8};
    if (v1==v2)
        cout << "v1 is equal to v2\n";
    else
        cout << "v1 is not equal to v2\n";
    if (d1==d2)
        cout << "d1 is equal to d2\n";
    else
        cout << "d1 is not equal to d2\n";
    if (v3<v1)
        cout << "v3 is less than v1\n";
    else
        cout << "v3 is not less than v1\n";
    if (d3<d1)
        cout << "d3 is less than d1\n";
    else
        cout << "d3 is not less than d1\n";
}

>,>=,<=,!= are similar. UPD: Also works with list.

  • »
    »
    9 years ago, # ^ |
      Vote: I like it +5 Vote: I do not like it

    what does v3<v1 mean?

    as i understand it is like string comparing, is this true ?

    • »
      »
      »
      9 years ago, # ^ |
        Vote: I like it +8 Vote: I do not like it

      Yes.We call that lexicographic order.

      Check this for more information.