VS2008窗體應用程序工程 之 vector使用示例 -- 升級功能,使用find

1、使用數組方式來給vector變量賦值。

頭文件:

#include <vector>
using namespace std;

示例:

vector <int> vectorL;
int szBuf[2] = {3, 8};
vectorL.reserve(2);
vectorL.assign(&szBuf[0], &szBuf[2]);
//vectorL.assign(szBuf, &szBuf[2]);
int nSum = 0;
for (vector<int>::iterator it = vectorL.begin(); it != vectorL.end(); it++)
{
	 nSum += *it;
}
printf(nSum.ToString());

2、查找是否存在某個數

頭文件:

#include <vector>
#include <algorithm>
using namespace std;

示例:

vector<int> vi;
int nLen = 5;
for (int i = 0; i < nLen; i++)
{
	 vi.push_back(i + 1);
}

vector<int>::iterator it = find(vi.begin(), vi.end(), 3);
if(it != vi.end())
{
	 printf("Find.");
}
else
{
	printf("NonExist !!!");
}

 

--- The End.

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章