#include <iostream>
#include <stack>
#include <queue>
#include <iterator>
#include <sstream>
#include <string>
using namespace std;
stack<int> st;
// push three elements into the stack
st.push(1);
st.push(2);
st.push(3);
st
@0x7f2275130488
// pop and print two elements from the stack
cout << st.top() << ' ';
st.pop();
cout << st.top() << ' ';
st.pop();
3 2
// modify top element
st.top() = 77;
// push two new elements
st.push(4);
st.push(5);
// pop one element without processing it
st.pop();
// pop and print remaining elements
while (!st.empty()) {
cout << st.top() << ' ';
st.pop();
}
cout << endl;
4 77
queue<string> q;
// insert three elements into the queue
q.push("Bob");
q.push("Sue");
q.push("Tim");
// read and print two elements from the queue
cout << q.front() << " ";
q.pop();
cout << q.front() << " ";
q.pop();
Bob Sue
// insert two new elements
q.push("Jasper");
q.push("Homer");
// skip one element
q.pop(); // alien abduction
// read and print two elements
cout << q.front() << " ";
q.pop();
cout << q.front() << endl;
q.pop();
Jasper Homer
q.push("Zoey");
// print number of elements in the queue
cout << "Number of people waiting in line: " << q.size() << endl;
Number of people waiting in line: 1
vector<int> v{1,2,3,4};
vector<int> w(4);
copy(v.rbegin(), v.rend(), w.begin());
w
{ 4, 3, 2, 1 }
for (auto it = v.rbegin(); it != v.rend(); ++it) {
cout << *it << " ";
}
cout << endl;
4 3 2 1
vector <int> v, w;
for(int i = 3; i <= 5; i++) {
v.push_back(i);
w.push_back(i * 10);
}
v
{ 3, 4, 5 }
w
{ 30, 40, 50 }
v.size()
3
copy(w.begin(), w.end(), v.begin());
v
{ 30, 40, 50 }
copy(w.begin(), w.end(), back_inserter(v));
v
{ 3, 4, 5, 30, 40, 50 }
deque <int> v, w;
for(int i = 3; i <= 5; i++) {
v.push_back(i);
w.push_back(i * 10);
}
v
{ 3, 4, 5 }
copy(w.begin(), w.end(), front_inserter(v));
v
{ 50, 40, 30, 3, 4, 5 }
vector <int> v, w;
for(int i = 3; i <= 5; i++) {
v.push_back(i);
w.push_back(i * 10);
}
v
{ 3, 4, 5 }
copy(w.begin(), w.end(), inserter(v, v.begin() + 1));
v
{ 3, 30, 40, 50, 4, 5 }
vector <int> v;
copy(istream_iterator <int>(cin),
istream_iterator <int>(), // works like end
back_inserter(v));
v
{ 12, 34, 56, 455, 7892, 234789, 78934 }
vector <int> v{0,1,2,3,4};
ostream_iterator <int> outIter(cout, " ");
copy(v.begin(), v.end(), outIter);
0 1 2 3 4
std::string paragraph = R"""(
It was the best of times
it was the worst of times
it was the age of wisdom
it was the age of foolishness
it was the epoch of belief
it was the epoch of incredulity
it was the season of Light
it was the season of Darkness
it was the spring of hope
it was the winter of despair
we had everything before us
we had nothing before us
we were all going direct to Heaven
we were all going direct the other way
in short
the period was so far like the present period
that some of its noisiest authorities insisted on its being received
for good or for evil
in the superlative degree of comparison only
)""";
std::stringstream stream(paragraph);
// count the number of times each word occurs in the input
std::map<string, size_t> word_count; // empty map from string to size_t
std::string word;
while (stream >> word) {
++word_count[word];
}
for (const auto &w : word_count)
cout << w.first << " occurs " << w.second << " times" << endl;
Darkness occurs 1 times Heaven occurs 1 times It occurs 1 times Light occurs 1 times age occurs 2 times all occurs 2 times authorities occurs 1 times before occurs 2 times being occurs 1 times belief occurs 1 times best occurs 1 times comparison occurs 1 times degree occurs 1 times despair occurs 1 times direct occurs 2 times epoch occurs 2 times everything occurs 1 times evil occurs 1 times far occurs 1 times foolishness occurs 1 times for occurs 2 times going occurs 2 times good occurs 1 times had occurs 2 times hope occurs 1 times in occurs 2 times incredulity occurs 1 times insisted occurs 1 times it occurs 9 times its occurs 2 times like occurs 1 times noisiest occurs 1 times nothing occurs 1 times of occurs 12 times on occurs 1 times only occurs 1 times or occurs 1 times other occurs 1 times period occurs 2 times present occurs 1 times received occurs 1 times season occurs 2 times short occurs 1 times so occurs 1 times some occurs 1 times spring occurs 1 times superlative occurs 1 times that occurs 1 times the occurs 14 times times occurs 2 times to occurs 1 times us occurs 2 times was occurs 11 times way occurs 1 times we occurs 4 times were occurs 2 times winter occurs 1 times wisdom occurs 1 times worst occurs 1 times