C++ string類和STL使用方法總結(jié)

string

if you want to string class, you have to include headfile string:

#include<string>

and use namespace std

using std::string;
  • 7 kinds of string constructors in C++98:
//the first one
string one;
//2nd 
string two("hello world!");
//3rd
string three(two);
//4th
string four(4,'$');
//5th
char alls[]="Hello, miaowu~!"
string five(alls,7);
//6th
string six(alls,alls+6);
string six2(&five,&five[6]);
//7th
string seven(six,0,3);
  • frequently used methods:

1.find

string strfind("i am a supercat!");
string strword("supercat");
char chword("supercat");
int pos = strfind.find(strword,0);
int pos2 = strfind.find(chword,1,7);

2.find_first_of find_last_of

string s1("cobra")
int where = s1.find_first_of("hark"); //where = 3

3.size()

string str("supercat");
cout << str.size() << endl;

4.+= append()

string str("i am");
str+= 'a';
str.append("cat");

5.operator+

string str1("a");
string str2("supercat");
string str3 = str1+str2;

6.assign()

string test;
string str("super supercat");
test.assign(str,0,5);//test is "super"
test.assgin(6,'$');//test is "$$$$$$"

7.insert()

string str("The cat.");
str.insert(4,"big ");
str.insert(str.size()-1," is lovely!!!",11);

8.erase()

string str("This is a big cat!");
str.erase(10,4)

STL

three frequently used kinds:

  • vector
#include<vector>
using std::vector;

methods or functions:

1.the same as string

//1st
vector<int> ve(3,9); // 9 9 9
ve.size();// 3
//2nd
ve.push_back(6);//9 9 9 6
//3rd
ve.erase(ve.begin(),ve.begin()+2);
//4th
ve.insert(ve.begin(),10);
ve.insert(ve.end(),2,100);

vector<int> anotherve(8,7);
ve.insert(ve.end(),anotherve.begin(),anotherve.begin()+3);

int myarrray[] = {4,5,6,7,8};
ve.insert(ve.begin(),myarray,myarray+3);

//5rd
assign()

2.STL special

vector<int> ve;
ve.push_back(3);
ve.pop_back();
ve[0];
ve.at(0);

3.vector sepcial

std::reverse(ve.begin(),ve.end());

4.iterator:

double d={3.0,8.7,9.3,5.7,2.5,3.4};
vector<double> scores(d,d+4);
vector<double>::iterator pd = scores.begin();
for(;pd!=scores.end();pd++)
  cout << *pd << endl;
  • list
    list special
int myarray[] = {0,1,2,3,4,5,6,7,8};
list<int> il;
list<int> ail;
ail.insert(ail.end(),myarray,myarray+8);
il.splice(il.begin(),ail);
il.remove(2);
il.sort();
ail.sort();
il.unique();
il.merge(ail);
  • set

1.construct

string str[] = {"one","two","three","four","five","six"};
set<string> setone(str,str+5);

2.copy

ostream_iterator<string,char> out (cout," ");
copy(setone.begin(),setone.end(),out);

3.set special

set_intersection()
set_difference()
set_union()
  • multimap

1.header file

#include<map>
using namespce std;

2.special

multimap<int,string> codes;// key: int    value:string
codes.insert(pair<const int , string>(415,"San Francisco"));
codes.insert(pair<const int , string>(510,"Oakland"));
codes.insert(pair<const int , string>(718,"Brooklyn"));
codes.insert(pair<const int , string>(718,"Staten Island"));
codes.insert(pair<const int , string>(415,"San Rafael"));
codes.insert(pair<const int , string>(510,"Berkeley"));
cout << codes.count(415) << endl;

3.iterator

multimap<int, string>::iterator it;
for(it= codes.begin(); it != codes.end();codes++)
    cout <<(*it ).first << " " << (*it).second << endl;

pair< multimap<int,string>::iterator , multimap<int,string>::iterator > range = codes.equal_range(718);
for(it = range.first;it != range.second;it++)
  cout << (*it).second << endl;
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • PushNotification發(fā)展歷史 iOS 10 中,把以前雜亂的和通知相關(guān)的 API 都統(tǒng)一了,現(xiàn)在開發(fā)者...
    Arackboss閱讀 1,179評論 0 0
  • 一個路人甲 手?jǐn)R著情話 想過捧鮮花 來不及送給她 一個路人甲 窩在屋檐下 無火無炊,亦無人牽掛 他生命還有大半 他...
    飲夢蕭蕭閱讀 369評論 0 4

友情鏈接更多精彩內(nèi)容