Sequence Containers
- uniform initialization (curly braces)
- code snippet
const int kNumInts = 200;
std::vector<int> vec(kNumInts);
std::generate(vec.begin(), vec.end(), rand);
std::sort(vec.begin(), vec.end());
std::copy(vec.begin(), vec.end(),
std::ostream_iterator<int>(cout, "\n"));
" vector is the type of sequence that should be used by default… deque is the data structure of choice when most insertions and deletions take place at the beginning or at the end of the sequence."
一C++ ISO Standard (section 23.1.1.2)
教材链接:http://web.stanford.edu/class/cs106l/full_course_reader.pdf
Associate Containers && Iterator
std::map<T1, T2> v.s std::unordered_map<T1, T2>
std::set<T> v.s std::unordered_set<T>
// Map/Set:sorted, faster to iterate through a range of elements.
// Unordered map/set: faster to access individual elements by key.
- Iterators allow iteration over any container, whether it is ordered or not.
auto it = std::find(vec.begin(), vec.end(), elemToFind);
if(it != vec.end()) cout << "Found" << endl;
else cout << "Not Found" << endl;
map.count(key);
std::find(map.begin(), map.end(), key); // find() is faster.
- list: doubly linked list.