首页 > STL编程 > STL vector示例代码

STL vector示例代码

STL vector示例代码:

fonrt()用于返回第一个元素,back()用于返回最后一个元素。当迭代器容量不够时,会翻倍先前的容量。

#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <iterator>
using namespace std;

int main()
{
	vector<string> sentence;
	
	sentence.reserve(5);
	
	sentence.push_back("Hello");
	sentence.push_back("how");
	sentence.push_back("are");
	sentence.push_back("you");
	sentence.push_back("?");
	
	copy(sentence.begin(), sentence.end(), ostream_iterator<string>(cout, " "));
	cout << endl;
	
	cout << "max_size():" << sentence.max_size() << endl;
	cout << "size():" << sentence.size() << endl;
	cout << "capacity():" << sentence.capacity() << endl;
	
	swap(sentence[1], sentence[3]);
	sentence.insert(find(sentence.begin(), sentence.end(), "?"), "always");
	sentence.back() = "!";
	
	copy(sentence.begin(), sentence.end(), ostream_iterator<string>(cout, " "));
	cout << endl;
	
	cout << "max_size():" << sentence.max_size() << endl;
	cout << "size():" << sentence.size() << endl;
	cout << "capacity():" << sentence.capacity() << endl;
	
	return 0;
}

 


觉得文章还不错?点击此处对作者进行打赏!


本文地址: 程序人生 >> STL vector示例代码
作者:代码疯子(Wins0n) 本站内容如无声明均属原创,转载请保留作者信息与原文链接,谢谢!


更多



分类: STL编程 标签: ,
  1. 张学连
    2011年11月18日13:53 | #1

    no matching function for call to ‘std::vector
    一般由定义的类型与存入的类型不匹配引起。

    [回复]

  1. 本文目前尚无任何 trackbacks 和 pingbacks.