Stl

Author Avatar
cooscao 3月 10, 2020

STL模版库基础使用

String字符串处理

#include <string>

初始化

string s; // 空字符串
string s1("12345678"); // 是"12345678"的副本
string s2(s1);
string s3 = s2;
string s4 = "hello";
string s5 (s3, 2, 5); //从第2个元素开始,复制5个
string s6 (5, 'X'); // 将5个'X'组成字符串
string s7 (s4, 4); // 从第4位开始取

char chs[] = "12345";
string s8 (chs, 3); // 对char[] 复制前3位

输出

12345678
12345678
12345678
hello
34567
XXXXX
o
123

访问元素和字符串长度

  • size() 和 length():这两个函数会返回 string 类型对象中的字符个数,且它们的执行效果相同。
  • 访问可以[]at,函数 at() 在使用时会检查下标是否有效。如果给定的下标超出字符的长度范围,系统会抛出 out_of_range 异常。
const std::string cS ("c.biancheng.net");
std::string s ("abode");
char temp =0;
char temp_1 = 0;
char temp_2 = 0;
char temp_3 = 0;
char temp_4 = 0;
char temp_5 = 0;
temp = s [2]; //"获取字符 'c'
temp_1 = s.at(2); //获取字符 'c'
temp_2 = s [s.length()]; //未定义行为,返回字符'\0',但Visual C++ 2012执行时未报错
temp_3 = cS[cS.length()]; //指向字符 '\0'
temp_4 = s.at (s.length ()); //程序异常
temp_5 = cS.at(cS.length ()); //程序异常
  • 修改下标字符串。下标操作符 [] 和函数 at() 均返回字符的“引用”
    char & r = s[2]; // 引用关系
    char* p = &s[3]; // 引用关系
    r = 'X';
    *p = 'Y';
    cout << s << endl;

    // 重新分配
    s = "12345678";
    r = 'X';
    *p = 'Y';
    cout << s << endl;

输出

abode
abXYe
12XY5678

比较

  • String类的字符串支持直接使用运算符进行比较,任一不能为NULL,否则程序异常退出。

字符串修改

截取
/** Substr 截取
     */

//s.substr(pos1,n)返回字符串位置为pos1后面的n个字符组成的串
string s2=s.substr(1,5);//bcdef

//s.substr(pos)//得到一个pos到结尾的串
string s3=s.substr(4);//efg
插入
string str="to be question";
string str2="the ";
string str3="or not to be";
string::iterator it;

//s.insert(pos,str)//在s的pos位置插入str
str.insert(6,str2);                 // to be the question

//s.insert(pos,str,a,n)在s的pos位置插入str中插入位置a到后面的n个字符
str.insert(6,str3,3,4);             // to be not the question

//s.insert(pos,cstr,n)//在pos位置插入cstr字符串从开始到后面的n个字符
str.insert(10,"that is cool",8);    // to be not that is the question

//s.insert(pos,cstr)在s的pos位置插入cstr
str.insert(10,"to be ");            // to be not to be that is the question

//s.insert(pos,n,ch)在s.pos位置上面插入n个ch
str.insert(15,1,':');               // to be not to be: that is the question

//s.insert(s.it,ch)在s的it指向位置前面插入一个字符ch,返回新插入的位置的迭代器
it = str.insert(str.begin()+5,','); // to be, not to be: that is the question

//s.insert(s.it,n,ch)//在s的it所指向位置的前面插入n个ch
str.insert (str.end(),3,'.');       // to be, not to be: that is the question...

//s.insert(it,str.ita,str.itb)在it所指向的位置的前面插入[ita,itb)的字符串
str.insert (it+2,str3.begin(),str3.begin()+3); // to be, or not to be: that is the question...

return 0;
删除
std::string str ("This is an example sentence.");
std::cout << str << '\n';
// "This is an example sentence."
str.erase (10,8);       //            ^^^^^^^^
//直接指定删除的字符串位置第十个后面的8个字符
std::cout << str << '\n';
// "This is an sentence."
str.erase (str.begin()+9);//           ^
//删除迭代器指向的字符
std::cout << str << '\n';
// "This is a sentence."
//       ^^^^^
str.erase (str.begin()+5, str.end()-9);
//删除迭代器范围的字符
std::cout << str << '\n';
// "This sentence."
追加和替换

appendreplace

将string转化为int

string s= "23"
int n = stoi(s, 0) // 从0位置开始

deque双向队列

#include <deque>

使用

//双向队列 deque
//by MoreWindows http://blog.csdn.net/morewindows
#include <deque>
#include <cstdio>
#include <algorithm>
using namespace std;
int main()
{
    deque<int> ideq(20); //Create a deque ideq with 20 elements of default value 0
    deque<int>::iterator pos;
    int i;

    //使用assign()赋值  assign在计算机中就是赋值的意思
    for (i = 0; i < 20; ++i)
        ideq[i] = i;

    //输出deque
    printf("输出deque中数据:\n");
    for (i = 0; i < 20; ++i)
        printf("%d ", ideq[i]);
    putchar('\n');

    //在头尾加入新数据
    printf("\n在头尾加入新数据...\n");
    ideq.push_back(100);
    ideq.push_front(i);

    //输出deque
    printf("\n输出deque中数据:\n");
    for (pos = ideq.begin(); pos != ideq.end(); pos++)
        printf("%d ", *pos);
    putchar('\n');

    //查找
    const int FINDNUMBER = 19;
    printf("\n查找%d\n", FINDNUMBER);
    pos = find(ideq.begin(), ideq.end(), FINDNUMBER);
    if (pos != ideq.end())
        printf("find %d success\n", *pos);
    else
        printf("find failed\n");

    //在头尾删除数据
    printf("\n在头尾删除数据...\n");
    ideq.pop_back();
    ideq.pop_front();

    //输出deque
    printf("\n输出deque中数据:\n");
    for (pos = ideq.begin(); pos != ideq.end(); pos++)
        printf("%d ", *pos);
    putchar('\n');
    return 0;
}

queue队列

#include <queue>
  • 入队: q.push(x) 添加到尾部
  • 出队: q.pop()弹出队列第一个元素,并不会返回值
  • 访问: q.front()访问最早被压入的元素,q.back()访问最后被压入的元素
  • 空: q.empty()

map

#include <map>
  • 初始化: map<int, int> dict, map<string, int> dict1
  • 插入: dict.insert(pair<int, int>(1, 2));dict[2] = 3;
  • 遍历:
map<int, string>::iterator iter;  

// iter->first, iter->second 获取值
for(iter = dict.begin(); iter != dict.end(); iter++)  

  cout<<iter->first<<' '<<iter->second<<endl;
  • 键是否存在与map中
map<int, string>::iterator iter;  

iter = dict.find(1);  

if(iter != dict.end())  

  cout<<"Find, the value is "<<iter->second<<endl;  

else  

  cout<<"Do not Find"<<endl;
  • 删除. erase