forked from Mooophy/Cpp-Primer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ex11_8.cpp
49 lines (43 loc) · 1.58 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
42
43
44
45
46
47
//! @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 5; 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 <map>
#include <string>
#include <algorithm>
int main()
{
std::vector<std::string> exclude = {"aa","bb","cc"};
std::string word;
while([&]()
{
std::cout << "enter:\n";
std::cin >> word;
return word != "@q";
}())
{
for(const auto &e : exclude)
if (e == word) std::cout << "excluded!";
}
return 0;
}