C ++ Standard Template Library --vector

1. In the C language, a lot of things to make their own implementation and realization is not good error-prone, and some complex operations to write is quite troublesome, therefore, C ++ provides us with Standard Template Library (Standard TemplateLibrary), which encapsulates many practical container, so we do not need effort to achieve their details and can directly call the function to achieve a lot of features, which is similar to Java in a variety of easy, for example, is a collection of List, HashMap these containers, with these containers we also becomes easy to manipulate data

2. Introduction The following is a vector of common usage

① vector is actually a length of the array can be changed, which is the length of the data were not sure of the situation is a good solution, we open up a different worry how long the array length, this vector can be declared directly

List similar set of Java, but the expression of how language is different, but the function is really like, we can use the vector in a manner adjoining table to store map, so you can not be afraid to use adjacency matrix pointers achieve adjacency list of readers is very friendly

② If you need to use the vector we need to add vector header files that #include <vector>, in addition also you need to add the following header file using namespace std; this code we can use the vector

③ vector is defined

Defines a single vector: vector <typename> name;

This definition is actually define the one-dimensional array name [size], but this size can be according to the situation specific changes, more space-saving, which typename may be any basic types, e.g. int, double, char, structure, etc. may be a standard STL containers, e.g. vector, set, queue, etc. Note that if a typename STL is easy when needed at the time defined by a space between >> symbols, because the standard C ++ 11 before the compiler will treat it as a shift operation results in an error, here are some simple examples:

vector<int> name;
vector<double> name;
vector<char> name;
vector<node> name;
vectot<vector<int> > name;

It may be defined as a vector array:

vector<typename> arrayName[arraysize];
例如:
vector<int> vi[100];

Such arrayName each element in [] is a vector, beginners can be a two-dimensional vector as a two-dimensional array can be a two-dimensional edge length of the array appreciated

Access to the container element ④ vector: vector access There are two ways, one is to be accessed by means of the subject matter, the other is to be accessed via the iterator manner

1) be accessed by the subscript

And array access common is the same, for <typename> of the container defines a vector vi, the direct access vi [index] to as vi [0], vi [1], of course, where the subscripts 1 vi.size () - 1 subscript visit outside of this range will be wrong

2) to be accessed by the iterator

Iteration may be understood as a pointer to something, which is defined vector <typename> :: iterator it;

In this way it is a vector <typename> :: iterator variable so that you can get the iterator it, we can access vector elements inside by * it

The following is a specific program:

#include<iostream>
#include<vector>
using namespace std;
int main(void){
	vector<int> vi;
	for(int i = 0; i < 5; ++i){
		vi.push_back(i);
	}
	//vi.begin()表示取vi的首元素地址,而it指向这个地址 
	vector<int> ::iterator  it = vi.begin();
	for(int i = 0; i < 5; ++i){
		cout << *(it + i) << " ";
	}
	return 0;
} 

 

vi.begin before ⑤ () function is the address of the first element vi taken, then there is a function end, the next address is taken out of the tail element address, end flag can serve as the end iterator does not store any of the elements, the following is specific code:

#include<iostream>
#include<vector>
using namespace std;
int main(void){
	vector<int> vi;
	for(int i = 0; i < 5; ++i){
		vi.push_back(i);
	}
	//vi.end()表示取vi的尾元素地址的下一个地址
	vector<int> ::iterator  it = vi.begin();
	for(it; it != vi.end(); it++){
		cout << *it << " ";
	}
	return 0;
} 

 

3. It is noted that STL containers may be used only in the vector and the string vi.begin () This iterator + 3 written integer plus

The following are examples of commonly used functions vector analysis:

① push_back (x): a vector is added after the element x, the time complexity is O (1), the following code:

#include<iostream>
#include<vector>
using namespace std;
int main(void){
	vector<int> vi;
	for(int i = 0; i <= 3; i++){
		vi.push_back(i + 1);		
	}
	for(int i = 0; i < vi.size(); i++){
		cout << vi[i] << " ";
	} 
	return 0;
} 

② pop_back()

pop_back () function can be used to delete the last element of the vector, the time complexity is O (1), the following is a specific code:

#include<iostream>
#include<vector>
using namespace std;
int main(void){
	vector<int> vi;
	for(int i = 0; i <= 3; i++){
		vi.push_back(i + 1);		
	}
	vi.pop_back();
	for(int i = 0; i < vi.size(); i++){
		cout << vi[i] << " ";
	} 
	return 0;
} 

③ size function is used to obtain the number of elements in the vector

④ clear()

To clear all of the elements in the vector, time complexity is O (N), where N is the number of elements in the vector, as follows:

#include<iostream>
#include<vector>
using namespace std;
int main(void){
	vector<int> vi;
	for(int i = 0; i <= 3; i++){
		vi.push_back(i + 1);		
	}
	vi.clear();
	cout << vi.size() << " ";
	return 0;
} 

⑤ insert function

INSERT (it, x) is used to insert an element x to the vector at any iterator it, the time complexity is O (N)

#include<iostream>
#include<vector>
using namespace std;
int main(void){
	vector<int> vi;
	for(int i = 0; i <= 3; i++){
		vi.push_back(i + 1);		
	}
	//将-1插入到vi[2]的位置 
	vi.insert(vi.begin() + 2, -1);
	for(int i = 0; i < vi.size(); i++){
		cout << vi[i] << " ";
	} 
	return 0;
} 

⑥ erase function

There are two ways: delete a single element, delete all the elements within a segment, the time complexity is O (N)

① delete individual items

ERASE (it) is deleted iterator it at the element, as follows:

#include<iostream>
#include<vector>
using namespace std;
int main(void){
	vector<int> vi;
	for(int i = 5; i <= 9; i++){
		vi.push_back(i);		
	}
	//删除8这个元素 
	vi.erase(vi.begin() + 3);
	for(int i = 0; i < vi.size(); i++){
		cout << vi[i] << " ";
	} 
	return 0;
} 

② remove all elements within a range

All membered erase (first, last) is deleted [first, last) in the following code:

#include<iostream>
#include<vector>
using namespace std;
int main(void){
	vector<int> vi;
	for(int i = 5; i <= 9; i++){
		vi.push_back(i);		
	}
	vi.erase(vi.begin() + 1, vi.begin() + 4);
	for(int i = 0; i < vi.size(); i++){
		cout << vi[i] << " ";
	} 
	return 0;
} 

4. vector common use:

① itself can be carried out using an array of violations, and in the case of the number of elements of uncertainty may well save space

Some applications require output to portions of the data based on some conditions in the same row, the intermediate data separated by white space, since the number of output data is uncertain, it may be used to store all the data vector output of all of the required output is then output in one

② use of adjacency tables to store FIG.

Guess you like

Origin blog.csdn.net/qq_39445165/article/details/93302530