C + + BASIC data structure collation

Source: Internet
Author: User
Tags case statement

Hash Map (unordered_map)
  • Insert
    #include <unordered_map>usingnamespace<charbool>  Hash_map;hash_map.insert (Make_pair<char,bool> ('a', true));
  • Find
    if (Hash_map.find (num) =="notfound" << Endl;
    Unordered_map <char, bool>:: Iterator got = Hash_map.find (' a ');
    Got->first point to Key
    Got->second point to Value

String
  1. How to traverse each element in a string
    // SA is a string  for string:: iterator p = sa.begin (); P!= Sa.end (); p++ )         << *p;      << Endl;

  2. How to use the Find in string

    string: Size_type pos = str.find ('0');        // POS is the position in string you find. if (pos==string::"cannt find"; Else  << pos;

Type Conversion
    1. Char-Int
      char s = ' 5 ' int a = s ' 0 ';
    2. String Int
      string s = std::to_string (5);
    3. String-Int
      std::string""; int //

    4. You Cann ' t-switch-case statement on string by C + +


Stack
  1. How to press into a pair
    int> > s;s.push (Make_pair (Root,1*tmp = s.top (). int len = S.top (). Second;
  2. With the push () in the STL stack, Pop (), Top ()

    //STL Stack adapters (stacks)#include <iostream>#include<stack>using namespacestd;intMain () {stack<int> is;//Defining Stack Objects   for(inti =0; I < -; ++i) is. Push (i+ -);//100~199 the stack in a single ordercout<<"Top element:"<< is. Top () <<endl;//view top of stack elements  is. Pop ();//out of stack operationcout<<"new top element:"<< is. Top () <<endl;//view top of stack elementscout<<"Size:"<< is. Size () <<endl;//View Stack Height  return 0; } 

Heap
  • How to use existing data structures to implement heap operations including Push_head, Pop_head, Sort_heap
    //Range Heap Example#include <iostream>//Std::cout#include <algorithm>//std::make_heap, std::p op_heap, std::p ush_heap, Std::sort_heap#include <vector>//std::vectorintMain () {intMyints[] = {Ten, -, -,5, the}; Std::vector<int> V (myints,myints+5);  Std::make_heap (V.begin (), V.end ()); Std::cout<<"initial max heap:"<< V.front () <<'\ n'; std::p op_heap (V.begin (), V.end ());                 V.pop_back (); //pop_heap () is used to eject the first element in the heap & #65292, and put it in the last position of the interval & #65292, and then re-build the previous element into a heap & #12290;Std::cout <<"Max heap after pop:"<< V.front () <<'\ n'; V.push_back ( About); std::p ush_heap (V.begin (), V.end ());//push_heap () is used to add the last element of the specified interval to the heap and make the entire interval a new heap & #12290; Note that all elements except the last element already constitute a heap & #12290;Std::cout <<"Max Heap after push:"<< V.front () <<'\ n';  Std::sort_heap (V.begin (), V.end ()); Std::cout<<"final sorted Range:";  for(Unsigned i=0; I<v.size (); i++) Std::cout<<' '<<V[i]; Std::cout<<'\ n'; return 0;} Output:initial Max Heap: -Max heap after pop: -Max Heap after push: Aboutfinal sorted Range:5 Ten  the  -  About

Array & Vector
  • How multidimensional vectors determine the size of rows and columns
    vector<vector<int> > matrixint rowsize = matrix.size (); int colsize = matrix[0].size ()
  • Inserts an element in front of a specified position using insert ()
    #include <iostream>#include<vector>using namespacestd;intmain () {vector<int> V (3); v[0]=2; v[1]=7; v[2]=9; //Insert 8 in front of the front elementV.insert (V.begin (),8); //inserting a new element before the second element 1V.insert (V.begin () +2,1); //Insert a new element at the end 1V.insert (V.end (),3);  for(vector<int>::iterator It=v.begin (); It!=v.end (); it++) cout<<*it<<Endl; System ("Pause"); return 0; }
  • The method of finding the smallest element in vector with STL
    cout<<"maxinum:"<<*min_element (V.begin (), V.end ()) <<Endl; int index = min_element (V.begin (), V.end ())-v.begin ();
  • Create a multidimensional array with vectors. Multidimensional arrays are written as vector<vector<int> spaces;. In fact, a two-dimensional array/matrix A is described. Its initialization can refer to the following code
     std::vector<std::vector<int  > >    A; Vector  < Vector<int  > > INTVV      ;      Vector  <int  > Intv;       int   I,j;  for  (I=0 ; I<10 ; ++i) {intv.clear ();  for  (J=0 ; J<10 ; ++j) intv.push_back (i  *10  +< Span style= "color: #000000;"        >J); Intvv.push_back (INTV);  
  • A simple notation for iterating through an array
    int num[] = {2,2,3};      for (int i:num)   // You must define I here.        cout << i << Endl;
  • The Pop/push in the vector
    #include <vector>using namespace std;vector<char> array;array.push_back (' 9 '); char num = Array.back (); Array.pop_back ();   

STL operator overloading to change the sort algorithm
Sort elements & #65292; object struct person   {person  }    (int id, const string& name, int age ): Id_ (ID), Name_ (name), Age_ (age)    {}        int id_;    String name_;    int age_;  };  Method 1& #65306; operator< used for sorting (written in function body)  bool operator< (const person& RT) {return this-> Id_ <sort (Members.begin (), Members.end ());//mode 2& #65306; write comparison function bool Compage (const person& PL, const person& PR) {return Pl.age_ <sort (Members.begin (), Members.end (), compage);//mode 3& #65306; functor struct Compname {bool operator () (const person& PL, const person& PR) {return Pl.name_ < /c22> 

Definition of data structure in Leetcode
 for singly-struct*     int*     listnode **     ListNode (int**/

C + + BASIC data structure collation

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.