CPP試験無料問題集「C++ Institute C++ Certified Professional Programmer 認定」

What happens when you attempt to compile and run the following code?
# include <iostream>
# include <map>
using namespace std;
int main() {
int t[] = { 1, 1, 2, 2, 3, 3, 4, 4, 5, 5 };
string s[] = { "one", "one", "two", "two", "three","three", "four", "four", "five", "five"}; map<int, string> m;
for (int i = 0; i < 10; i++) {
m.insert(pair<int, string>(t[i], s[i]));
}
if (m.count(3) == 2) {
m.erase(3);
}
for (map<int, string>::iterator i = m.begin(); i != m.end(); i++) {
cout << i?>first << " ";
}
return 0;
}

What will happen when you attempt to compile and run the code below, assuming that you enter the following sequence: true false<enter>?
# include <iostream>
# include <string>
using namespace std;
int main ()
{
bool a,b;
cin>>boolalpha>>a>>b;
cout<<a<<b<<endl;
return 0;
}
Program will output:

What happens when you attempt to compile and run the following code?
# include <vector>
# include <set>
# include <iostream>
# include <algorithm>
using namespace std;
template<class T>struct Out {
ostream & out;
Out(ostream & o): out(o){}
void operator() (const T & val ) { out<<val<<" "; } };
struct Sequence {
int start;
Sequence(int start):start(start){}
int operator()() { return start++; } };
int main() {
vector<int> v1(10);
generate_n(v1.begin(), 10, Sequence(1));
random_shuffle(v1.rbegin(), v1.rend());
sort(v1.begin(), v1.end(), great<int>());
for_each(v1.begin(), v1.end(), Out<int>(cout));cout<<endl;
return 0;
}
Program outputs:

What will happen when you attempt to compile and run the code below, assuming that you enter the following sequence: 1 2 3 4 quit<enter>?
# include <iostream>
# include <string>
# include <list>
# include <algorithm>
using namespace std;
template<class T>struct Out {
ostream & out;
Out(ostream & o): out(o){}
void operator() (const T & val ) {out<<val<<" "; } };
int main ()
{
list<string> l;
while(cin.good())
{
string s;
cin>>s;
if (s == "quit") break;
l.push_back(s);
}
for_each(l.begin(), l.end(), Out<string>(cout));
return 0;
}
Program will output:

What happens when you attempt to compile and run the following code?
# include <deque>
# include <iostream>
# include <algorithm>
using namespace std;
template<class T>struct Out {
ostream & out;
Out(ostream & o): out(o){}
void operator() (const T & val ) { out<<val<<" "; } };
int main() {
int t[]={8, 10, 5, 1, 4, 6, 2, 7, 9, 3};
deque<int> d1(t, t+10);
sort(d1.begin(), d1.end());
deque<int>::iterator it = upper_bound(d1.begin(), d1.end(), 4);
for_each(it, d1.end(), Out<int>(cout));cout<<endl;
return 0;
}
Program outputs:

What happens when you attempt to compile and run the following code?
#include <list>
#include <iostream>
using namespace std;
bool mycomparison (int first, int second){return first>second;}
template<class T>
void print(T start, T end) {
while (start != end) {
std::cout << *start << " "; start++;
}
}
int main()
{
int t1[] ={ 1, 7, 8, 4, 5 };
list<int> l1(t1, t1 + 5);
int t2[] ={ 3, 2, 6, 9, 0 };
list<int> l2(t2, t2 + 5);
l1.sort(mycomparison);
l2.sort(mycomparison);
l1.merge(l2,mycomparison);
print(l1.begin(), l1.end());
print(l2.begin(), l2.end()); cout<<endl;
return 0;
}

What happens when you attempt to compile and run the following code?
# include <vector>
# include <iostream>
# include <algorithm>
using namespace std;
template<class T>struct Out {
ostream & out;
Out(ostream & o): out(o){}
void operator()(const T & val ) {
out<<val<<" ";
}
};
struct Sequence {
int start;
Sequence(int start):start(start){}
int operator()() { return start++; } };
int main() {
vector<int> v1(10);
vector<int> v2(10);
generate(v1.begin(), v1.end(), Sequence(1));
random(v1.begin(),v1.end());
for_each(v1.begin(), v1.end(), Out<int>(cout) );cout<<endl;
return 0;
}
Program outputs:

What happens when you attempt to compile and run the following code?
#include <iostream>
#include <algorithm>
#include <deque>
#include <vector>
using namespace std;
bool identical(int a, int b) {
return b == 2*a?true:false;
}
int main() {
int t[] = {1,2,3,2,3,5,1,2,7,3,2,1,10, 4,4,5};
int u[] = {2,4,6,4,6,10,2,4,14,6,4,2,20,8,8,5};
vector<int> v1(t, t + 15);
deque<int> d1(u, u + 15);
pair<deque<int>::iterator, vector<int>::iterator > result;
result = mismatch(d1.begin(), d1.end(), v1.begin(), identical); //Line I if (result.first == d1.end() && result.second == v1.end()) {//Line II
cout<<"Identical\n";
} else {
cout<<"Not identical\n";
}
return 0;
}
Program outputs:

What happens when you attempt to compile and run the following code?
#include <iostream>
#include <algorithm>
#include <vector>
# include <deque>
# include <set>
using namespace std;
void myfunction(int i) {
cout << " " << i;
}
int main() {
int t[] = { 10, 5, 9, 6, 2, 4, 7, 8, 3, 1 };
vector<int> v1(t, t + 10);
deque<int> d1(t, t + 10);
set<int> s1(t, t + 10);
for_each(v1.begin(), v1.end(), myfunction); // Line I
for_each(d1.begin(), d1.end(), myfunction); // Line II
for_each(s1.begin(), s1.end(), myfunction); // Line III
return 0;
}

What happens when you attempt to compile and run the following code?
include <iostream>
#include <algorithm>
#include <vector>
#include <deque>
#include <set>
using namespace std;
int main() {
int t[] = { 10, 5, 9, 6, 2, 4, 7, 8, 3, 1 };
vector<int> v1(t, t + 10);
deque<int> d1(t, t + 10);
set<int> s1(t, t + 10);
cout<<find(v1.begin(), v1.end(), 6)<<" "<<find(d1.begin(), d1.end(), 6)<<"
"<<find(s1.begin(), s1.end(), 6);
return 0;
}

What happens when you attempt to compile and run the following code?
#include <iostream>
#include <algorithm>
#include <vector>
#include <set>
using namespace std;
void myfunction(int i) { cout << " " << i;
}
struct sequence {
int val,inc;
sequence(int s, int i):val(s),inc(i){}
int operator()(){
int r = val; val += inc;
return r;
}
};
int main() {
vector<int> v1(10);
fill(v1.begin(), v1.end(), sequence(1,1));
for_each(v1.begin(), v1.end(), myfunction);
return 0;
}
Program outputs: