C++里面的vector提供了对array/list的操作功能, 如下代码演示了如何建立vector并进行增加元素/查找元素/删除元素/枚举元素, 还演示了如何应用模板(template)来实现通用数据的vector打印.
#define _CRTDBG_MAP_ALLOC
#include <stdlib.h>
#include <crtdbg.h>
#include "stdafx.h"
#include <stdio.h>
#include <iostream>
#include <vector>
using namespace std;
template<typename _T>
void printVector(_T pvvtSrc) {
cout << endl;
for (_T::iterator lvnItem = pvvtSrc.begin(); lvnItem != pvvtSrc.end(); lvnItem++) {
cout << *lvnItem << " ";
}
cout << endl << "vector size: " << pvvtSrc.size() << endl;
}
template<class _C>
void printVectorC(_C pvvtSrc) {
cout << endl;
for (_C::iterator lvnItem = pvvtSrc.begin(); lvnItem != pvvtSrc.end(); lvnItem++) {
cout << (*lvnItem).data() << " ";
}
cout << endl << "vector size: " << pvvtSrc.size() << endl;
}
void testVectorChars() {
vector<string> lvvtChars;
lvvtChars.push_back("one");
lvvtChars.push_back("two");
lvvtChars.push_back("three");
printVectorC(lvvtChars);
//查找元素
vector<string>::iterator lvnItem = find(lvvtChars.begin(), lvvtChars.end(), "three");
if (lvnItem != lvvtChars.end()) { //找到,则删除
lvvtChars.erase(lvnItem);
}
printVectorC(lvvtChars);
}
void testVector() {
vector<int> lvvtInts;
//增加元素
lvvtInts.push_back(5);
lvvtInts.push_back(4);
lvvtInts.push_back(3);
lvvtInts.push_back(1);
printVector(lvvtInts);
//查找元素
vector<int>::iterator lvnItem = find(lvvtInts.begin(), lvvtInts.end(), 3);
if (lvnItem != lvvtInts.end()) { //找到,则删除
lvvtInts.erase(lvnItem);
}
printVector(lvvtInts);
lvvtInts.insert(find(lvvtInts.begin(), lvvtInts.end(), 1), 2); //在1的前面插入2
printVector(lvvtInts);
lvvtInts.insert(find(lvvtInts.begin(), lvvtInts.end(), 2), 3); //在2的前面插入3
lvvtInts.insert(find(lvvtInts.begin(), lvvtInts.end(), 2), 3); //在2的前面插入3
printVector(lvvtInts);
//删除所有3
for (vector<int>::iterator lvnItem = lvvtInts.begin(); lvnItem != lvvtInts.end(); lvnItem++) {
if (*lvnItem != 3)continue;
lvnItem=lvvtInts.erase(lvnItem);
lvnItem--;
}
printVector(lvvtInts);
}
int main(int argc, char* argv[])
{
_CrtSetDbgFlag(_CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF);
testVector();
testVectorChars();
getchar();
return 0;
}
上面代码, 由于string是一个类,所以template<typename _T>不通用, 只能重新用template<class _C>定义一个类相关的模板.
运行结果:
5 4 3 1
vector size: 4
5 4 1
vector size: 3
5 4 2 1
vector size: 4
5 4 3 3 2 1
vector size: 6
5 4 2 1
vector size: 4
one two three
vector size: 3
one two
vector size: 2
----------------
vector 与数组的互相转换:
//测试小数数组与vector的互相转换
void testFloatArrsVector() {
float lvflItems[] = { 1.1f,2.2f,3.3f,4.4f,5.5f };
// float arrays to vector
vector<float> lvvfItems (lvflItems, lvflItems + sizeof(lvflItems) / sizeof(float));
printVector(lvvfItems);
//亦可以通过下标直接访问元素
for (size_t i = 0; i < lvvfItems.size(); i++) {
cout << lvvfItems[i] << endl;
}
//vector to float arrays
if (!lvvfItems.empty()){
float* lvflNew = new float[lvvfItems.size()];
memcpy(lvflNew, &lvvfItems[0], lvvfItems.size() * sizeof(float));
//print float array
for (size_t i = 0; i < lvvfItems.size(); i++) {
cout << lvflNew[i] << " ";
}
delete[]lvflNew;
cout << endl;
// 或者直接指向vector的内存块,将其转换为数组来用.
float *lvflpt = &lvvfItems[0];
for (size_t i = 0; i < lvvfItems.size(); i++) {
cout << lvflpt[i] << " ";
}
}
}
从上述代码演示了从数组转vector和从vector转数组, 在vector转数组用了两种方法, 一种是先将数据做了一份拷贝再使用, 另一种方法是可以直接用 &lvvfItems[0]和lvvfItems.size()作为数组数据来用了.