The features of Vector in C++
2012-10-09 10:59
351 查看
There are so many containers in C++, like vector, list, map and so on. To be honest, I am always confused with these containers since in my point of view, they are somehow similar. But with no doubt, there still exist differences
among them. I try to expose these differences by using my experiences. I will start withvector.
1. In order to use vector, we need to add
#include <vector>
using namespace std;
in front.
2. How to define a vector
vector<type> v;
////type could be int, double.....
3. How to insert an element
v.push_back(3);
4. Accessing the elements.
Since vector could be regarded as a dynamic array, so we could use index of vector to access the elements. For instance,v[1], v[2].
Alternatively, we could use
iterator.
5. Advantanges and Disadvantanges of vector.
Ad: (1) It's easy to access the elements in vector since we could use index.
(2) It's easy to insert element at the end.
Disad: time-consuming to insert or remove element at positions other than the end.
among them. I try to expose these differences by using my experiences. I will start withvector.
1. In order to use vector, we need to add
#include <vector>
using namespace std;
in front.
2. How to define a vector
vector<type> v;
////type could be int, double.....
3. How to insert an element
v.push_back(3);
4. Accessing the elements.
Since vector could be regarded as a dynamic array, so we could use index of vector to access the elements. For instance,v[1], v[2].
Alternatively, we could use
iterator.
5. Advantanges and Disadvantanges of vector.
Ad: (1) It's easy to access the elements in vector since we could use index.
(2) It's easy to insert element at the end.
Disad: time-consuming to insert or remove element at positions other than the end.
相关文章推荐
- How do you copy the contents of an array to a std::vector in C++ without looping? (From stack over flow)
- The environment of C++ Programing in Linux
- The mixed programming in terms of matlab and C++
- Question 6: Which of the following are container adapters in the STL (Standard Template Library) in C++?
- The C in C++(Chapter 3 of Thinking in C++)
- Question 15: Which of the following statements describe the results of executing the code snippet below in C++?
- (NOT CLEAR)Question 18: In the given C++ code snippet, which of the following statements correctly identify how Mon of enum DOW
- An implementation of the skip list data structure written in C++
- In which of the following does a C++ developer use the placement new syntax to make new allocate an
- An Overview Of The New Services, Controls, And Features In ASP.NET 2.0
- Question 16: Which of the following methods can a developer use to override the default terminate() function in C++?
- An easy instance of using the template class in C++
- Reflection in C++: The simple implementation of Splinter Cell
- Question 19: Which of the following are not pre-processor directives in C++?
- GIT BASH IN INTELLIJ IDEA 13 ON WINDOWS One of the top features of the recent release of IntelliJ ID
- Question 38: What is the output of the program below in C++?
- The Usage of Lambda and Heap in the C++ STL
- How Would You Get the Count of an Array in C++?
- /*程序员面试宝典*/Which of the following statements describe the results of executing the code snippet below in C++?
- How To Call Run() Method of the Microsoft Script Control in C++