C++中的push_back函数

push_back是算法语言里面的一个函数名。如c++中的vector头文件里面就有这个push_back函数,在vector类中作用为在vector尾部加入一个数据。

string中也有这个函数,作用是字符串之后插入一个字符。

//basic_string_push_back.cpp
//compilewith:/EHsc
#include <string>
#include <iostream>
int main()
{
using namespace std;
string str1("abc");
basic_string<char>::iterator str_Iter,str1_Iter;
cout<<"The original string str1 is:";
for(str_Iter=str1.begin();str_Iter!=str1.end();str_Iter++)
cout<<*str_Iter;
cout<<endl;
str1.push_back('d');
cout<<"The modified string str1 is:";
for(str_Iter=str1.begin();str_Iter!=str1.end();str_Iter++)
cout<<*str_Iter;
cout<<endl;
}

输出:

1

2

The original string str1 is:abc

The modified string str1 is:abcd

猜你喜欢

转载自blog.csdn.net/kathy_paul/article/details/81501580