std::vector example

 

#include <stdio.h>
#include <stdlib.h>
#include <vector>
#include <iostream>
#include <algorithm>

using namespace std;

int main()
{
	std::vector<int> line;
	for (int i = 0; i < 10; i++) 
	{ 
		
		int temp = (i%2)? 5*i: -5*i ;
		line.push_back(temp); 
	}

	std::cout << "using integer as loop iterator: " << std::endl;
	for (int i = 0; i < 10; i++)
	{
		std::cout << line[i] << " ";
	}
	std::cout << std::endl;


	std::cout << "using vector<int> :: iterator as loop iterator: " << std::endl;
	std::cout << "Increasing Sorting " << std::endl;
	sort(line.begin(), line.end());
	vector<int>::iterator it;
	for (it = line.begin(); it < line.end(); it++)
	{
		std::cout << *it << " ";
	}
	std::cout << std::endl;

	std::cout << "Decreasing Sorting " << std::endl;
	reverse(line.begin(), line.end());
	for (it = line.begin(); it < line.end(); it++)
	{
		std::cout << *it << " ";
	}
	std::cout << std::endl;


	std::cout << "find example in std::vector<int> " << std::endl;

	int value = 25;
	vector<int>::iterator result = find(line.begin(), line.end(), value);

	if (result == line.end() ) // Not find
		std::cout << value << " could NOT be found " << std::endl;
	else
		std::cout << value << " Found " << std::endl;

	std::cin.get();

	return 0;
}

 

 

 

posted @ 2017-03-02 16:22  souwang  阅读(219)  评论(0编辑  收藏  举报