vector使用总结

1.vector的遍历

#include "stdafx.h"
#include <iostream>
#include <vector>
using namespace std;
void main(void)
{
	vector<char> Myvector;
	vector<char>::iterator MilkshakeIterator;//声明迭代器
	Myvector.push_back('1');
	Myvector.push_back('2');
	Myvector.push_back('3');
	Myvector.push_back('4');
	Myvector.push_back('5');
	Myvector.push_back('6');
        for (MilkshakeIterator = Myvector.begin(); MilkshakeIterator != Myvector.end(); ++MilkshakeIterator)//迭代器移动
	{
		cout << *MilkshakeIterator << " ";
	}
        //for (int i=0; i<<span style="font-family: Arial, Helvetica, sans-serif; font-size: 12px;">Myvector</span><span style="font-size: 12px; font-family: Arial, Helvetica, sans-serif;">.size(); ++i) {</span><span style="font-size: 12px; font-family: Arial, Helvetica, sans-serif;">	</span><span style="font-size: 12px; font-family: Arial, Helvetica, sans-serif;">cout << Myvector[i] << ' ';</span><span style="font-size: 12px; font-family: Arial, Helvetica, sans-serif;">	</span><span style="font-size: 12px; font-family: Arial, Helvetica, sans-serif;">}</span>
	system("pause");
}



结果将按次序输出1 2 3 4 5 6

2.vector的size,maximum size,capacity

#include "stdafx.h"
#include <iostream>
#include <vector>
using namespace std;
typedef vector<int> INTVECTOR;
void main()
{
	INTVECTOR thevector;
	thevector.push_back(42);
	cout << "thevector's size is: " << thevector.size() << endl;
	cout << "thevector's maximum size is: " << thevector.max_size() << endl;
	cout << "thevector's capacity is: " << thevector.capacity() << endl;
	thevector.resize(1000);
	cout << endl << "After reserving storage for 1000 elements:" << endl;
	cout << "thevector's size is: " << thevector.size() << endl;
	cout << "thevector's maximum size is: " << thevector.max_size() << endl;
	cout << "thevector's capacity is: " << thevector.capacity() << endl;
	thevector.resize(2000);
	cout << endl << "After resizing storage to 2000 elements:" << endl;
	cout << "thevector's size is: " << thevector.size() << endl;
	cout << "thevector's maximum size is: " << thevector.max_size() << endl;
	cout << "thevector's capacity is: " << thevector.capacity() << endl;

	system("pause");
}

结果如下图


可见,如果vector使用时不resize的话,vector的大小是动态增长的。

3.vector的foreach语句

#include "stdafx.h"
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
void PrintIt(char* & StringToPrint) { cout << StringToPrint << endl; }
void main(void)
{
	vector<char* > FruitAndVegetables;
	FruitAndVegetables.push_back("carrot");
	FruitAndVegetables.push_back("pumpkin");
	FruitAndVegetables.push_back("potato");
	FruitAndVegetables.push_back("apple");
	FruitAndVegetables.push_back("pineapple");
	for_each(FruitAndVegetables.begin(), FruitAndVegetables.end(), PrintIt);

	system("pause");
}

结果见下图


4.注意vector的end

#include "stdafx.h"
#include <iostream>
#include <vector>
using namespace std;
typedef vector<int> INTVECTOR; //定义了一个宏
void main()
{
	INTVECTOR thevector; //定义一个vector变量
	INTVECTOR::iterator theIterator; //采用迭代器技术
	thevector.push_back(42); //插入一个元素值为42的元素到vector末尾中
	thevector.push_back(1); //插入一个元素值为1的元素到vector末尾中
	thevector.push_back(109); //插入一个元素值为109的元素到vector末尾中
	thevector.pop_back();  //删除109
	cout << "thevector [ ";
	for (theIterator = thevector.begin(); theIterator != thevector.end(); theIterator++)  //打印结果
	{
		cout << *theIterator; if (theIterator != thevector.end() - 1) cout << ", ";
	}
	cout << " ]" << endl;
	cout << *(thevector.end() - 1);

	system("pause");
}

结果如下图


可见thevector.end() - 1才指向thevector的最后一个元素。

5.count方法

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
void main(void)
{
	vector<int> Scores;
	Scores.push_back(100);
	Scores.push_back(80);
	Scores.push_back(45);
	Scores.push_back(75);
	Scores.push_back(99);
	Scores.push_back(100);
	int NumberOf100Scores(0);
	NumberOf100Scores = count(Scores.begin(), Scores.end(), 100);
	cout << "There were " << NumberOf100Scores << " scores of 100" << endl;

	system("pause");
}



6.count_if方法

#include "stdafx.h"
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
class IsAToothbrush
{
public:
	IsAToothbrush(string& InToothbrushCode) : ToothbrushCode(InToothbrushCode) {}
	bool operator() (string& SalesRecord)
	{
		return SalesRecord.substr(0, 4) == ToothbrushCode;
	}
private:
	string ToothbrushCode;
};
void main(void)
{
	vector<string> SalesRecords;
	SalesRecords.push_back("0001 Soap");
	SalesRecords.push_back("0002 Shampoo");
	SalesRecords.push_back("0003 Toothbrush");
	SalesRecords.push_back("0004 Toothpaste");
	SalesRecords.push_back("0003 Toothbrush");
	string VariableToothbrushCode("0003");
	int NumberOfToothbrushes(0);
	NumberOfToothbrushes = count_if(SalesRecords.begin(), SalesRecords.end(),
		IsAToothbrush(VariableToothbrushCode));
	cout << "There were  " << NumberOfToothbrushes << " toothbrushes matching code " << "0003" << " sold" << endl;
	system("pause");
}

7.find方法

#include "stdafx.h"
#include <vector> 
#include <algorithm>
#include <iostream>
using namespace std;
void main(void)
{
	vector<char* > Fruit;
	vector<char* >::iterator FruitIterator;
	Fruit.push_back("Apple");
	Fruit.push_back("Pineapple");
	Fruit.push_back("Star Apple");
	FruitIterator = find(Fruit.begin(), Fruit.end(), "Pineapple");
	if (FruitIterator == Fruit.end())
	{
		cout << "Fruit not found in vector" << endl;
	}
	else { cout << *FruitIterator << endl; }
	system("pause");
}

8.find_if方法

#include "stdafx.h"
#include <string>
#include <vector>
#include <algorithm>
#include <iostream>
using namespace std;
class EventIsIn1997
{
public:
	bool operator () (string& EventRecord)
	{
		return EventRecord.substr(12, 4) == "1997";
	}
};
void main(void)
{
	vector<string> Events;
	Events.push_back("07 January  1995  Draft plan of house prepared");
	Events.push_back("07 February 1996  Detailed plan of house prepared");
	Events.push_back("10 January  1997  Client agrees to job");
	Events.push_back("15 January  1997  Builder starts work on bedroom");
	Events.push_back("30 April    1997  Builder finishes work");
	vector<string>::iterator EventIterator = find_if(Events.begin(), Events.end(), EventIsIn1997());
	if (EventIterator == Events.end()) { cout << "Event not found in vector" << endl; }
	else { cout << "Event found in vector" << endl; }
	system("pause");
}

9.search方法

#include "stdafx.h"
#include <string>
#include <vector>
#include <algorithm>
#include <iostream>
using namespace std;
void main(void)
{
	vector<char> TargetCharacters;
	vector<char> vectorOfCharacters;
	TargetCharacters.push_back('');
	TargetCharacters.push_back('');
	vectorOfCharacters.push_back('1');
	vectorOfCharacters.push_back('2');
	vectorOfCharacters.push_back('');
	vectorOfCharacters.push_back('');
	vector<char>::iterator PositionOfNulls =
		search(vectorOfCharacters.begin(), vectorOfCharacters.end(),
		TargetCharacters.begin(), TargetCharacters.end());
	if (PositionOfNulls != vectorOfCharacters.end()) cout << "We found the nulls" << endl;
	system("pause");
}

10.insert方法

#include "stdafx.h"
#include <iostream>
#include <vector>
using namespace std;
void main(void)
{
	vector<int> vector1;
	for (int i = 0; i < 10; ++i)
		vector1.push_back(i);
	vector1.insert(vector1.begin(), -1);
	vector1.insert(vector1.end(), 10);
	int IntArray[2] = { 11, 12 };
	vector1.insert(vector1.end(), &IntArray[0], &IntArray[2]);
	vector<int>::iterator Itera;
	for (Itera = vector1.begin(); Itera != vector1.end(); ++Itera)
		cout << *Itera << endl;
	system("pause");
}

结果将输出
-1
0
1
2
.
.
.
10
11
12

11.erase方法

#include "stdafx.h"
#include <vector>
#include <iostream>
using namespace std;
void main(void)
{
	vector<int> vector1;   // define a vector of integers
	for (int i = 0; i < 10; ++i)
		vector1.push_back(i);
	cout << "vector contains " << vector1.size() << " elements" << endl;
	vector1.pop_back();     // erase the last element 9
	cout << "vector contains " << vector1.size() << " elements" << endl;
	vector1.erase(vector1.begin());  // erase the first element (1) using an iterator
	cout << "vector contains " << vector1.size() << " elements" << endl;
	vector1.erase(vector1.begin(), vector1.end());  // erase all the remaining elements
	cout << "vector contains " << vector1.size() << " elements" << endl;
	system("pause");
}



12.使用std::sort对vector排序

#include<algorithm>

例如对下面的vector容器进行排序

vector<pair<pair<intint>, int>>ve; 

可以这样

  1. bool UDless(pair<pair<intint>,int> elem1, pair<pair<intint>,int> elem2)  
  2. {  
  3.     return elem1.second < elem2.second;  
  4. }  

然后调用sort函数

 sort(ve.begin(), ve.end(),UDless);  



版权声明:

原文地址:https://www.cnblogs.com/walccott/p/4957088.html