C + + pen questions common STL and other small operation use summary __c++

Source: Internet
Author: User

Strings String substr Action:

String S ("Lewis");
Returns a string containing the copy default 0-s.size ()
s.substr (pos,n) of n characters starting from Pos in S;
ways to change string:
String S ("Lewis");
S.insert (Pos,args); Insert before POS note here POS uses subscript, and an iterator will make an error.
s.erase (Pos,len);   Delete
s.assign (args) from Pos;     Using Args's content to replace s original string returns the reference
S.append (args) of S;
S.replace (Range,args)//range is a subscript and a length, or a pair of iterators of S,
//args can be a string str 
//     or can be a 
str,pos,len//     CP, (len)
//     n,c n characters c
//     b,e  pair of iterators
//Can be said to be very flexible


conversion between string and numeric value:
To_string (val);//Converts the value Val to a string, Val can be any arithmetic type
stoi (s,p,b);//Returns the number of the starting substring of s (representing the integer content), B is the base for the conversion, and the default is 10,p is the size_t pointer, Used to hold the subscript of the first digit in S, p default 0 is not to save subscript
STOL (s,p,b);
Stof (S,P,B); L:long  f:float d:double
stod (s,p,b);

Association container set Map
Determines whether the element to be processed already exists in the set:

C.find (k); Returns an iterator that points to the element with the first keyword K and, if K is not in the container, returns the iterator ==c.end ()
C.count (k);//Returns the number of elements that have a keyword equal to K ==0 the set is empty
c.equal _range (k);/returns the pair of an iterator that represents the range of elements that have a keyword equal to K, and if K does not exist, the two members of the pair are equal to C.end ()

Flexible selection of map operation subscript and find
map<string,int>word_cnt;
word_cnt["Hello"];//use the subscript operator, if "Hello" is not in the map, the value initialization is used to give the int an initial 0
word_cnt.find ("Hello"); Hello map is not generated automatically

control output bit number

#include <iomanip>  //need to include this header file
cout << "a=" << setprecision (2) << a <<endl;//a= 0.2124  will output 0.2
//If you want to keep the decimal point after several:
cout.setf (ios::fixed);  0
cout << "a=" <<fixed<< setprecision (2) << a <<endl;//output a=0.20  
for insufficient digits COUT.UNSETF (ios::fixed); Cancel the collar.

flexible use of ceil () floor () to take the whole up and down

int a=2;
cout<<a/3<<endl;
Cout<<ceil (A/3) <<endl;
Cout<<ceil ((a+0.0)/3) <<endl;
Cout<<floor ((a+0.0)/3) <<endl;

>>>0
>>>0
>>>1
>>>0

keep in mind that you want to maintain the type uniformity between the values of the operations. Especially when designing int and long

Vector<long long>value;
When using accumulate summation
if (Accumulate (Value.cbegin (), Value.cend (), 0))
    cout<< "wrong!" <<endl;
Because value is a long long and the sum of the initial value 0 is the int type, you must use 0LL common 0L 0F 0D
if (accmulate (Value.cbegin), Value.cend (), 0LL))
cout<< "right" <<endl;

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.