美文网首页
C++ string类和STL使用方法总结

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

作者: i_Eloise | 来源:发表于2018-01-19 20:57 被阅读0次

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;

相关文章

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

    string if you want to string class, you have to include h...

  • STL:string类的函数总结

    STL:string类的函数总结

  • C++STL整理

    C++ STL中最基本以及最常用的类或容器string、vector、set、list、map string 处理...

  • C++的STL库

    c++的STL库(即标准模板库),简单来说就是帮你总结数据结构和算法,以实现代码的复用的类库。stl库的相关概念:...

  • STL string类

    1. 为何需要使用string类 std::string和std::wstring分别模拟了普通字符串和宽字符串,...

  • 触宝内推-substring-c++

    push_back使用方法C++中string.find()函数与string::npos

  • C++ STL string

    官方文档整理:referencecppreference

  • STL容器 算法 迭代器 适配器

    STL包括容器,算法和迭代器 STL的模板类为c++提供了完善的数据结构,它的模板类的样式就好象数据结构中用类c或...

  • string in c++

    string 类是 STL 中 basic_string 模板实例化得到的模板类。其定义如下:typedef ba...

  • fork 之写时复制(COW)

    参考 COW奶牛!Copy On Write机制了解一下C++ STL STRING的COPY-ON-WRITE技...

网友评论

      本文标题:C++ string类和STL使用方法总结

      本文链接:https://www.haomeiwen.com/subject/kunooxtx.html