算法中里面的一个函数名,如c++中的vector头文件里面就有这个push_back函数,在vector类中作用为在vector尾部加入一个数据。
string中也有这个函数,作用是字符串之后插入一个字符。
如果是指标准模板库(stl)中容器的一般pushback()操作函数,那么是指在容器尾端插入一项数据,比如
vector<int> a(10);
a.pushback(10);
那么a的尾端,同时也是唯一一个数据a[0]就会为设置为10。

函数原型
1
voidpush_back(value_type_Ch);
参数
1
_Ch-->Thecharactertobeaddedtotheendofthestring.
在vector类中:
1
2
3
4
voidpush_back(const_Ty&_X)
{
insert(end(),_X);
}
在vector<_Bool, _Bool_allocator>类中:
1
2
3
4
voidpush_back(constbool_X)
{
insert(end(),_X);
}

举例:
//basic_string_push_back.cpp
//compilewith:/EHsc
#include <string>
#include <iostream>
int main()
{
using namespace std;
string str1("abc");
basic_string<char>::iteratorstr_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');
str1_Iter = str1.end();
str1_Iter--;
cout<<"The last char acter-letter of the modified str1 is now:"
<<*str1_Iter<<endl;
cout<<"The modified string str1 is:";
for(str_Iter=str1.begin();str_Iter!=str1.end();str_Iter++)
cout<<*str_Iter;
cout<<endl;
}

输出结果将是在abc的最后一个元素c后面增加一个元素d:abcd
Logo

权威|前沿|技术|干货|国内首个API全生命周期开发者社区

更多推荐