push_back使用方法

算法中里面的一个函数名,如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
  • 13
    点赞
  • 45
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值