Self-test 6:
Standard Template Library (STL)
-
What is the output of this program?
#include <iostream> #include <deque> #include <algorithm> #include <iterator> using namespace std; int main() { deque<int> intDeq; ostream_iterator<int> screen(cout," "); intDeq.push_back(13); intDeq.push_back(75); intDeq.push_back(28); intDeq.push_back(35); copy(intDeq.begin(),intDeq.end(),screen); cout<<endl; intDeq.push_front(0); intDeq.push_back(100); copy(intDeq.begin(),intDeq.end(),screen); cout<<endl; }
Solution13 75 28 35
0 13 75 28 35 100
-
Fill in the codes required into the []
#include <iostream> #include <list> #include <algorithm> #include <iterator> using namespace std; int main() { list <int> intList1,intList2; ... //assume intList1 contain 23 58 58 58 36 15 93 98 58 [1] // copy intList1 to intList2; [2] // sort intList2 [3] // remove duplicates in the sorted intList2 (use the unique function) ``` }
Solution[1]: intList2 = intList1;
[2]: intList2.sort()
[3]: intList2.unique();
-
What is meant by vector in the container library contains?
- It is a sequence container that encapsulates dynamic size arrays
- It is a sequence container that encapsulates static size arrays
- It manages the memory
- None of the mentioned
SolutionA
vector in the container library contains sequence container that manipulates and encapsulates dynamic size arrays.
-
Try to predict the output of the program
#include <iostream> #include <vector> using namespace std; int main() { vector<int> v; v.assign( 10, 42 ); for (int i = 0; i < v.size(); i++) { cout << v[i] << " "; } }
Solutionoutput 42 for 10 times
-
What is the output of this program?
#include <iostream> #include <list> #include <queue> using namespace std; int main() { queue<char> q; q.push('a'); q.push('b'); q.push('c'); cout << q.front(); q.pop(); cout << q.front(); q.pop(); cout << q.front(); q.pop(); }
Solutionabc
-
What is the output of this program?
#include <list> #include <string> #include <iostream> using namespace std ; typedef list<string> LISTSTR; int main() { LISTSTR :: iterator i; LISTSTR test; test.insert(test.end(), "one"); test.insert(test.end(), "two"); LISTSTR test2(test); LISTSTR test3(3, "three"); LISTSTR test4(++test3.begin(), test3.end()); cout << "test:"; for (i = test.begin(); i != test.end(); ++i) cout << " " << *i << endl; cout << "test:"; for (i = test2.begin(); i != test2.end(); ++i) cout << " " << *i << endl; cout << "test:"; for (i = test3.begin(); i != test3.end(); ++i) cout << " " << *i << endl; cout << "test:"; for (i = test4.begin(); i != test4.end(); ++i) cout << " " << *i << endl; }
Solutiontest: one
two
test: one
two
test: three
three
three
test: three
three
-
Consider the following statements, can you tell which element is position pointed to in [1] and [2] and what is their position in charList?
char cList[10]={'a','i','C','d','e','f','o','H','u','j'}; vector<char> charList(cList,cList+10); vector<char>::iterator position; position = find(charList.begin(),charList.end(),'d'); //[1] position = find_if(charList.begin(),charList.end(),isupper); //[2]
Solution[1]: 'd', position is 3, [2]: 'C', position is 2
[1] search charList for the first occurrence of 'd', [2]use function find_if to find first uppercase character
-
Try to predict the output marked by []
int intList[15]={12,34,56,34,34,78,38,43,12,25,34,56,62,5,49}; vector<int> vecList(intList,intList+15); int List[2]={34,56}; vector<int>::iterator location; location = search(vecList.begin(),vecList.end(),List,List+2); cout<<(location-vecList.begin())<<endl; [1] sort(vecList.begin(),vecList.end()); bool found; found = binary_search(vecList.begin(),vecList.end(),78); if (found) cout<<"found"<<endl; [2] else cout<<"NA"<<endl; [2]
Solution[1]: 1, [2]: found
[1] use search to find the position of subsequence list occurs in vecList, [2] use binary_search to to search vecList and return bool value
-
Fill in codes required into the []
char cList[10]={'A','a','A','B','A','c','D','e','F','A'}; vector<char> charList1(cList,cList+10); vector<char>::iterator lastElem; ostream_iterator<int> screen(cout," "); [1]//remove A from charList1 copy(charList1.begin(),lastElem,screen); [2] //replace A with Z in charList1
Solution[1]: lastElem = remove(charList1.begin(),charList1.end(),'A');
[2]: replace(charList1.begin(),charList1.end(),'A','Z');
-
What is the output of this program?
stack<int> intstack; intStack.push(16); intStack.push(8); intStack.push(20); intStack.push(3); while (!intStack.empty()) { cout<<intstack.top()<<" "; intstack.pop(); }
Solution3 20 8 16
-
what is the output of the following program?
#include <iostream> #include <vector> int main () { std::vector<int> myvector (5); // 5 default-constructed ints std::vector<int>::reverse_iterator rit = myvector.rbegin(); int i=0; for (rit = myvector.rbegin(); rit!= myvector.rend(); ++rit) *rit = ++i; std::cout << "myvector contains:"; for (std::vector<int>::iterator it = myvector.begin(); it != myvector.end(); ++it) std::cout << ' ' << *it; std::cout << '\n'; return 0; }
Solutionmyvector contains: 5 4 3 2 1
The range between vector::rbegin and vector::rend contains all the elements of the vector (in reverse order).