C++ string类和STL使用方法总结

2018-01-19  本文已影响0人  i_Eloise

string

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

#include<string>

and use namespace std

using std::string;
//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);

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:

#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;
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);

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()

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;
上一篇 下一篇

猜你喜欢

热点阅读