C + + standard library and STL

Source: Internet
Author: User

There are three important standard libraries in C + +, string, vector, Bitset, each of which is a class, and the corresponding namespace is std. The object of the string class can store a string, corresponding to the way in which the string is stored in C, the advantage of C + + is that you can create an object without specifying the length, in connection and delete, you only need to use the "+" and "-" after the operator is overloaded. The vector class object can store an array, can be int,char,string, and so on, when used, like a stack, through Push_back, Pop_back and other operations, this is not the same as the general array of places.

String class

1 string + "" OK

2 string + string OK

3 "" + String OK

string S1 = "Test";

String s2 = S1 + "Orange" + "Apple";

string s3 = "Orange" + s2 + "banana";

String S4 = s3 + "a"

including, sequential containers, associative containers, iterators, algorithms, etc. wherevectors and lists belong to the sequential container,map,list, Multimapandmultiset belong to the associative container.

Vector
  • The bottom of the vector is implemented with a dynamic array, which supports subscript operations.
  • Push_back is actually a copy of the element that is put in, so the element has the ability to replicate.
  • When creating N objects with a vector (in the formof avector VEC (n, Class)), use the class class () creates a temporary object and then copies the temporary object to the VEC using the copy constructor. space.
  • Vectors have no push_front.
  • in , size is the number of elements stored, resize Span style= "Font-family:times New Roman;" >vector reserve can change the maximum capacity.
  • vector memory allocation policy:  
    a)   capacity 0 When developing a vec n also for .  
    B)   when full, the element is placed again, capacity twice times the original.
  • the bottom of the list is a linked list, which does not support subscript operations.
  • List has push_front,push_back and other operations.
  • A map is a pair container that stores multiple pair.
  • the elements in map are arranged from small to large according to key .
  • the underlying implementation of map is the use of a binary tree, usually using a red-black tree.
  • Map Requirements for containers:thekey type must support comparisons, while value does not require.
  • The reference operation for a key in map is:map (key), which returns an lvalue.
  • When you use the subscript to access a key that does not exist in the map , a new keyis added, andthevalue For an initialization value (for value of the class type ,It is initialized with the default constructor, initialized to 0 for the built-in type ).
  • The subscript operation, in addition to being used for insertion, can also be used to access a keythat already exists to update value.
  • The key value of map is not changed.
  • Countand theFindthe difference. Countonly for viewingMapis there aKey(forMap,CountThe return value is0or1,0represents thisKeydoes not exist,1represents a presence). Findused to view aKeythe location (through the. End ()The comparison to get thisKeydoes it exist in thisMap, if theMap, you can read aKeycorresponding to thevalue).
  • In addition to the use of subscript, the insert operation can also be usedInsert. Insertis a return value of typepair, the first parameter is an iterator, and the second parameter indicates whether the insert succeeds. If thisKeynot inMap, the second parameter is returned as atrue, indicating that the insertion was successful;Keyin theMap, the second parameter is returned as afalse, indicating that the insert failed and is not updated at this timevaluevalues.
  • the set bottom is implemented by red and black trees and sorted by value.
  • the elements in the set are unique (because set is a collection).
List Mapsetmultimap

A key value corresponds to more than one value, using a similar usage to map

Unordered_map

It is a class template in the C++11boost library, similar to a hash table, and is used like a map

Some issues to be aware of
  • Map> VEC;This definition is wrong, because>>is a symbol, not a two>symbol, and in the template, is the<as the start character,>is Terminator, so in the definition above, the compiler cannot read the>Terminator, guideTo the wrong
    cannot be used directlyVECInitializeList, nor can it be used directlyListInitializeVEC,Queueas well. However, an iterator can be used to specify aListto initialize a section of space in theVECclass, or you can specify an object by using an iteratorListto initialize a section of space in theVECthe object of the class.
  • You also need to insert the container, and the insert operation inserts a copy of an object or variable, so the elements inside the STL container must support copying and assigning values.
  • When you delete an element in a container, it causes the iterator for that element to become invalid, so you should use an iterator to receive the return value when using erase .

If the size is specified in 3vector, the array is used , and the Push-back () is not properly placed in the a[i] = "";

If no size is specified, push is available , and the operation can take an array

Example:#include <iostream>

#include <string>

#include <vector>

using namespace Std;

int main () {

Vector<string> words;

Words.push_back ("Zero");

Words.push_back ("one");

cout<<words[0]<<endl;

int x;

cin>>x;

Cout<<words.size () <<endl;

Switch (x) {

Case 0:cout<<words[0]<<endl;

Break

Case 1:

cout<<words[1]<<endl;

Break

Case 2:

cout<<words[2]<<endl;

Break

Default

Break

}

}

#include <iostream>

#include <string>

#include <vector>

using namespace Std;

int main () {

Vector<string> words (9);

Words[0] = "zero";

WORDS[1] = "one";

cout<<words[0]<<endl;

int x;

cin>>x;

Switch (x) {

Case 0:cout<<words[0]<<endl;

Break

Case 1:

cout<<words[1]<<endl;

Break

Case 2:

cout<<words[2]<<endl;

Break

Default

Break

}

}

Map of STL

1map Additions and Queries

#include <iostream>

#include <map>

#include <string>

using namespace Std;

int main () {

Map<string,int> M1;

m1["Beijing"] = 100;

m1["Shenzhen"] = 200;

m1["Shanghai"] = 300;

cout<<m1["Shanghai"]<<endl;

Map<string,int>::iterator it = M1.begin ();

while (It! = M1.end ()) {// this compares

cout<<it->first<< "" <<it->second<<endl;

it++;

}

return 0;

}

2 whenever you use the subscript to access the map element, if the element does not exist, then the first time in the map is reborn into a key-value pair. So using subscript to access non-existent key-value pairs increases the size of the container, and the order of the keys in the map is generally sorted alphabetically.

#include <iostream>

#include <map>

#include <string>

using namespace Std;

void print (const map<string,int>::value_type &p) {

cout<<p.first<< "" <<p.second<<endl;

}

int main () {

Map<string,int> Word_count;

string Word;

while (Cin>>word) {

if (Word = "abc") {

word_count[word]++;

}

Else

Break

}

Map<string,int>::iterator it = Word_count.begin (); Common ways to traverse a map

while (It! = Word_count.end ()) {

cout<<it->first<< "" <<it->second<<endl;

it++;

}

return 0;

}

Word_count.insert (Map<string,int>::value_type ("abc", 10)); INSERT statement in Map

Map<string,int>::iterator it1 = Word_count.find ("abc"); Lookup statement in //map

Word_count.erase ("abc"); DELETE statement

Set is similar to map , you cannot insert two identical values

C + + standard library and STL

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.