C ++ Interview Questions and notes finishing (a) ------- STL and generic programming

Here are some possible to test himself in the interview topics encountered in the learning process, to be summed up here:

STL and generic programming part

1. STL and generic programming relationship

2. What is the STL six components , respectively, what role?

Container part

Container and associated container order :
on the order of container and associated container, some things do not write easy to forget, in this strengthening it:

(1) vector<int>a, assuming a five elements, and now you want to delete the second element of how to write? In between the first element and the second element into a 3 it?

(2) When using sequential containers, try to use vector with vector, to note that, vector container is not suitable for middle repeatedly delete or add elements, vector only suitable for frequently at the end of add / delete elements, If you have to intermediate element is added, what are the two solutions?

Which order of several container structure (3) C ++ in a total, which is what two C ++ 11 Added?

(4) vector<vector<int>>ahow represent two-dimensional array of rows and columns?

(5) shows how the maximum value of the vector container, for example, find vector<int>numthe maximum and minimum;

(6) associated with the container, map, set, multimap and multiset which is stable sort?

(7) How to achieve the underlying associative containers set?

Adapter part
1.STL provides three container adapter: stack, queue, priority_queue, the specific process is how to achieve?

Algorithms section

Iterator part

1. What is an iterator?


Answers

STL and generic programming

1. STL and generic programming relationship

STL: Standard Template Library-- Standard Template Library, STL is a set of template objects for handling various containers. STL demonstrates a programming model - generic programming.

2. What is the STL six components, namely what role?
STL six major components:
the algorithm (Algorithm)
is a function of the template data used in the operation of the vessel.
For example, the STL with sort () to the data a vector of the sorting, find () to search for an object in list, regardless of the structure and type of the function itself and their operation data, so they can be from simple array to use any highly complex data structure of the container;

The container (Container)
is a data structure, such as a list, vector, and deques, to provide a method of template class. In order to access data containers, may be used iterator class output from the container;

Iterator (the Iterator)
provides access to a container object. For example, a pair of iterators specified vector or list objects in a certain range. Iterator like a pointer. In fact, C ++ pointer is also an iterator. However, the iteration may also be those defined operator * () and other operators like pointers to a method of an object class;

Functor (Functor)
functor (functor) also known as the function object (function object), it is actually overloaded () operator struct, nothing special place

Container adapter (Adapter)
adapter is the sequence for the container underlying data structure, further encapsulated to adapt to the container application scenarios. STL provides three adapters, respectively, stack, queue and priority_queue.

Space dispenser (allocator)
mainly work includes 2 memory acquisition and release of two parts 1. Object creation and destruction

Container part

Container and associated container order :
Answer:
(1) vector<int>a, assuming a five elements, and now you want to delete the second element of how to write? In between the first element and the second element into a 3 it?

A: a.erase(a.begin()+1)ora.erase(a.begin()+1,a.begin()+2);

    a.insert(a.begin()+1,3);

(2) When using sequential containers, try to use vector with vector, to note that, vector container is not suitable for middle repeatedly delete or add elements, vector only suitable for frequently at the end of add / delete elements, If you have to intermediate element is added, what are the two solutions?

A: 1. the rear plus can then use the sort () function to sort
2. List can be used first, the copy in the List Vector.

Which order of several container structure (3) C ++ in a total, which is what two C ++ 11 Added?
A: vector, list, forward_list, deque , array, string. forward_list and array is new, compared to the built-in array array and safer to use.

(4) vector<vector<int>>ahow represent two-dimensional array of rows and columns?
A: The number of lines mat.size (), the number of columns to mat [0] .size;

(5) shows how the maximum value of the vector container, for example, find vector<int>numthe maximum and minimum;

A: As for this algorithm, the header file <algorithm>inside. And using the algorithm inside max_element min_element function may return an iterator most value inside the container, because the iterator is a complex pointer, the pointer value can be obtained best value, such as:

#include<algorithm>
#include<vector>
#include<iostream>
using std::vector;
using std::max_element;//do not forget
using std::min_element;
int main(){
     vector<int>num{1,2,3,4,5};
     vector<int>::iterator max=max_element(num.begin(),num.end());
     vector<int>::iterator min=min_element(num.begin(),num.end());
     std::cout<<"最大值为"<<*max<<std::endl;
     std::cout<<"最小值为"<<*min<<std::endl;
}

(6) associated with the container, map, set, multimap and multiset which is stable sort?

A: map and set a stable sort, multimap and multiset not a stable sort.

(7) How to achieve the underlying associative containers set?

A: How to achieve the underlying associative containers set?

Iterator
1. What is an iterator?

An iterator is a generalized pointer, in fact, it may be a pointer may be a pointer to its similar operations performed (such as contact reference operator * () and incremental Operator ++ ()) of the object. By pointer generalized into iterators, so that STL provides a unified interface for different containers.

Is a type of iterator typedef iterator is called, the scope for the entire class.

PS: Some containers can be used not only to represent the iterator element values, elements may be represented by an element index value, such as vector can be simultaneously represented by the subscript and iterator.
But in fact most of the container is only to invoke the container element values by using iterators.

References:
https://blog.csdn.net/u014744118/article/details/50727193

Guess you like

Origin blog.csdn.net/alexhu2010q/article/details/81744036