C++11 STL Summary + (Boost library) __c++

Source: Internet
Author: User
Tags data structures float double string format

Using a fast one-year STL library, decided to write about the use of this year's experience and summary. 1. Overview

Personal feel STL = = {algorithm, container, iterator};

type header File
Algorithm Include < algorithm >
Iterators Include < iterator >
Container include< map/vector/set/list/queue/...>

There's a very classical word on the C++primer. The algorithm operates the container, but does not change the container. “”

This operation container: refers to traversing the container through the iterator.
Does not change the container: means does not change the nature of the container, the most important is size.
However: the algorithm does not change the container, and sometimes it must change the size of the container. Who's going to do this? Answer: iterators. For example: Back_inserter

Effective C + + has a sentence: Do not use outdated iterators.

Iterators expire (fail). Any insertion on a vector, or deletion, may cause the iterator to fail. For example, if you insert an element from the middle, the subsequent iterator (pointer) is invalidated. As a result, data is moved in memory.
Some people think that push_back will not cause the iterator to fail: this is even less careful, assuming the vector to expand Now, push_back after all the original iterator are ineffective.
At this point you use find,copy algorithm, manipulate the invalid iterator, to return to the results you expect, the results can be imagined. 1.1 Data Structure

+-+-+-+-+-+-+-+-+-+-+-+-+-+-Split Line-+-+-+-+-+-+-+-+-+–+-+-+-+-
What data structures are provided in STL: Linear table (List of lists, dynamic array vector)
Include Red and black trees (Set, map), hash table (Unordered_map, Unordered_set)
And the two-fork heap/Priority Queue (Priority_quene), this data structure supports queue-jumping, which is the high priority task, inserted into the corresponding position of the task queue. The time complexity of inserting operation of this data structure is O (Logn), while in the linear table, the time complexity of inserting operation is O (n).
Queues (queue), stacks (stack)
+-+-+-+-+-+-+-+-+-+-+-+-+-+-Split Line-+-+-+-+-+-+-+-+-+–+-+-+-+- 2, Algorithm

··· 3. Container

··· 4. Iterator

There is a classic application in the I/O in the iterator
1-read a series of numbers/letters/strings from input into a specific container.

Vector<int> Ivec;
Copy (istream_iterator<int> (CIN),istream_iterator<int> (), Back_inserter (Ivec));
or by
vector<int> Ivec (CIN),istream_iterator<int> (istream_iterator<int>);
Istream_iterator<int> ()//the equivalent of EOF

+-+-+-+-+-+-+-+-+-+-+-output to screen +-+-+-+-+-+-+-+-+-+-+-
copy ( Ivec.begin (), Ivec.end (),ostream_iterator<int> (cout, "T"));  

Copies the Ivec from begin to end to the output buffer, which consists of the element \ t
/output buffer of Ivec: element 1\t, Element 2\t, element 3\t,...... When printing, \ t converts to tab output to screen

This article continues to be updated until October 17.

Here is a general form of using STL:

#include <iostream> #i

Nclude <algorithm> #include <vector> using namespace std;

BOOL Isold (int val) {return val%2;}


BOOL Calf (int nn) {return nn>7;}


void disp (int nn) {cout << nn << Endl;}
    int main () {vector<int> vec={1,7,3,9,5};
    Vector<int> vec2={5,9,3,11};
    bool h = std::all_of (Vec.begin (), Vec.end (), isold); cout << "All of VEC are old?
    "< 

Example two: Statistics the frequency of words appearing

/*************************************************************************
     File Name:main.cpp
     Author: Bin.wang
     Mail:   sa615168@mail.ustc.edu.cn
     Created time:fri June 2017 09:03:37 PM CST
 *************** /

#include <iostream>
#include <map >
#include <unordered_map>
#include <fstream>
using namespace std;
int main ()
{
    unordered_map<int,int> result  ;
    FStream infile ("./number.txt");
    int temp;
    while (infile >> temp) {
        result[temp]++;   Usage from C++primer c++11,stl
    } for

    (auto Num:result) {
        if (Num.second = = 1) cout<<  Num.first << Endl;
    }
    return 0;


}

Instance three pairs of strings with a specified character split

#include <iostream> #include <vector> using namespace std; #本例采用c ++11 Implementation std::vector<std::string> splitfunc (const std::string &str,const std::string) {s
    Td::vector<std::string> Resvec;
    if ("" = = str) {return resvec;
    } std::string STRs = str + delimiter;
    size_t Pos =strs.find (delimiter);
    size_t length = Strs.size ();
        while (POS!= std::string::npos) {std::string elem = strs.substr (0,pos);
        Resvec.push_back (Elem);
        Update Current string & pos STRs = Strs.substr (pos+1,length);
    pos = Strs.find (delimiter);

return Resvec;
    int main (int argc, char * * argv) {std::string label= "1,2,3,4";
    std::string delimiter = ",";
    std::vector<std::string> result;
    result = Splitfunc (Label,delimiter);
    for (auto S:result) {cout << s << endl;

return 0; //------------------Boost implementation-------------------------INclude <boost/algorithm/string.hpp> #include <iostream> using namespace std;
    int main (int argc, char **argv) {vector<string> result;
    Boost::split (Result, "1*6,7*2,3,4", boost::is_any_of (", *"));
    for (auto S:result) {cout << s<< Endl;
return 0; }
Trick

A number is often used in C + + to go to a string.

In fact, 2 of the most commonly used, the first kind of stringstream
The usage is Dtype a=4; stringstream ss; string result; SS << A; SS >> Result; This result stores the string format of the corresponding number.
where dtype can be int float double Longlong several.
The second is: Std::to_string (VAR) can be implemented directly, returning the string type.

Reference: http://www.cnblogs.com/biyeymyhjob/archive/2012/07/22/2603525.html

Related Article

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.