forked from pezy/CppPrimer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ex11_8.cpp
41 lines (39 loc) · 1.63 KB
/
ex11_8.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
//! @Alan
//!
//! Exercise 11.8:
//! Write a program that stores the excluded words in a vector
//! instead of in a set. What are the advantages to using a set?
// copied from the post on stack overflow:
// 1.No matter what elements you add or remove (unless you add
// a duplicate, which is not allowed in a set), it will always
// be ordered.
// 2.A vector has exactly and only the ordering you explicitly
// give it. Items in a vector are where you put them. If you put
// them in out of order, then they're out of order; you now need
// to sort the container to put them back in order.
// 3.However, if you are constantly inserting and removing items
// from the container, vector will run into many issues.
// 4.The time it takes to insert an item into a vector is proportional
// to the number of items already in the vector. The time it takes
// to insert an item into a set is proportional to the log of the
// number of items. If the number of items is large, that's a huge
// difference. Log(100,000) is 17; that's a major speed improvement.
// The same goes for removal.
// http://stackoverflow.com/questions/8686725/what-is-the-difference-between-stdset-and-stdvector
//!
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
int main()
{
std::vector<std::string> exclude = {"aa", "bb", "cc"};
for (std::string word; std::cin >> word;) {
if (std::find(exclude.begin(), exclude.end(), word) != exclude.end())
std::cout << "excluded!" << std::endl;
else
exclude.push_back(word);
}
for (auto const& s : exclude) std::cout << s << " ";
std::cout << std::endl;
}