10696 - I2P(II)2015 final exam Cheatsheet   

Description

1. How to make cin faster?

std::ios_base::sync_with_stdio(false);

 

2. An example of using <vector>

#include <iostream>

#include <vector>

#include <algorithm> // sort

int main ()

{

     std::vector<int> V;

     int x;

     while (std::cin>>x) {

          V.push_back(x);

     }

     for (auto t : V) std::cout<<" "<< t;

     std::cout << "\n";

     std::sort(V.begin(), V.end());

     for (auto t : V) std::cout<<" "<< t;

     std::cout << "\n";

}

 

3. An example of using <queue>
#include <iostream>

#include <algorithm>

#include <queue>

#include <string>

// Queueing

using namespace std;

int main()

{

     string s;

     queue<string> q;

     while (cin >> s) {

          if (s=="Push") {

               cin >> s;

               q.push(s);

          }

          else if (s=="Pop") {

               if (q.size()>0)

                    q.pop();

          }

          else if (s=="Front") {

               if (q.size()==0)

                    cout << "empty\n";

               else

                    cout<<q.front()<<"\n";

          }

     }

}

 

4. An example of using <stringstream>
#include <string>
#include <iostream>
#include <sstream> // std::stringstream
int main () {

     std::stringstream ss;
     ss << "Hello " << 123;
     std::string s = ss.str();
     std::cout << s << '\n';
}

 

5. An example of using <set>
#include <iostream>
#include <set>
int main()
{
     std::set<int> S = {1, 2, 3, 4};
     auto search = S.find(2);
     if(search != S.end()) {
          std::cout << (*search) << '\n';
     }
     else {
          std::cout << "Not found\n";
     }

}

 

6. An example of using <stack>

#include <stack>
#include <iostream>
int main()
{
     std::stack<int> s;
     s.push( 3 ); s.push( 6 );
     s.push( 18 );
     std::cout<<s.size()<<" elements\n";
     std::cout<<"Top: "<< s.top()<< "\n";
     s.pop();
     std::cout<<s.size()<<" elements\n";
}

7. An example of getline()

#include <string>
#include <iostream>
int main()
{
     std::string name;
     std::cout << "What is your name? ";
     std::getline(std::cin, name);
     std::cout << "Hello "<< name<< "\n";
}

 

8. An example of using <map>

#include <iostream>
#include <string>
#include <map>
#include <utility> // make_pair
int main()
{
     std::map<std::string, int> ID;
     ID.insert(std::make_pair("Tom", 123));
     std::cout << ID["Tom"] << "\n";
}

Input

  

Output

Sample Input  Download

Sample Output  Download

Tags




Discuss