1. 程式人生 > >【小練習】STL模板與容器_容器2

【小練習】STL模板與容器_容器2

1.練習程式碼-vector容器常規操作

#include "stdafx.h"
#include <iostream>
#include <stdio.h>
#include <vector>
#include <algorithm>
using namespace std;

void print(vector<int>);

int _tmain(int argc, _TCHAR* argv[])
{
	vector<int> array_v;
	array_v.push_back(1);
	array_v.push_back
(6); array_v.push_back(6); array_v.push_back(3); vector<int>::iterator itor; vector<int>::iterator itor2; itor = array_v.begin(); array_v.erase(remove(array_v.begin(), array_v.end(), 6), array_v.end()); print(array_v); return 0; } void print(vector<int> v) { cout <<
"\n vector size is: " << v.size() << endl; vector<int>::iterator p = v.begin(); }

2.關鍵點分析

2.1處理過程

#include "stdafx.h"
#include <iostream>
#include <stdio.h>
#include <vector>
#include <algorithm>
using namespace std;

void print(vector<int>);

int _tmain
(int argc, _TCHAR* argv[]) { vector<int> array_v; array_v.push_back(1); //在容器尾部新增資料1 array_v.push_back(6); //在容器尾部新增資料6 array_v.push_back(6); //在容器尾部新增資料6 array_v.push_back(3); //在容器尾部新增資料3 vector<int>::iterator itor; //定義指向容器資料的指標p itor = array_v.begin(); //將p指向容器資料的頭部 array_v.erase(remove(array_v.begin(), array_v.end(), 6), array_v.end()); //使用erase方法和remove方法進行數字6刪除 print(array_v); //列印刪除2個6之後的容器大小 return 0; } void print(vector<int> v) { cout << "\n vector size is: " << v.size() << endl; vector<int>::iterator p = v.begin(); }

2.2執行結果

容器2