C++的向量vector::push_back用法

看程序的时候遇到了“向量后推函数”,功能没怎么看懂,查了下手册,做个功能普及。

void push_back (const value_type& val);

Add element at the end1

Adds a new element at the end of the vector, after its current last element. The content of val is copied (or moved) to the new element.
在向量的最后一个元素之后的向量的末尾添加一个新元素。
val的内容被复制(或移动)到新元素。

This effectively increases the container size by one, which causes an automatic reallocation of the allocated storage space if -and only if- the new vector size surpasses the current vector capacity.
这有效地将容器大小增加了一个,这会导致分配的存储空间自动重新分配,前提是(且仅当)新向量大小超过当前向量容量时。

搞些代码试试

#include "stdafx.h"
#include <iostream>
#include <string>
#include <vector>

int main()
{
	char myfavorate[50] = "My favorite blog is:";
	char blog[] = "lab_3D";
	std::vector<char> myvector={ myfavorate ,myfavorate+21 };
	std::cout << myfavorate <<"\n";
	
	for (int i = 0;i < sizeof(blog);i++)
		myvector.push_back(blog[i]);

	for (int k = 0; k < myvector.size(); k++)
			std::cout << myvector[k] ;
	std::cout << "\n";

    return 0;
}

输出结果:
在这里插入图片描述


  1. http://www.cplusplus.com/reference/vector/vector/push_back/ ↩︎

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值