CPP 無料問題集「C++ Institute C++ Certified Professional Programmer」

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

What will happen when you attempt to compile and run the following code?
# include <iostream>
# include <map>
# include <vector>
# include <sstream>
# include <string>
using namespace std;
int main() {
int t[] = { 3, 4, 2, 1, 0, 3, 4, 1, 2, 0 };
vector<int> v(t, t + 10);
multimap<int, string> m;
for (vector<int>::iterator i = v.begin(); i != v.end(); i++) {
stringstream s;s << *i << *i;
m.insert(pair<int, string>(*i, s.str()));
}
pair<multimap<int, string>::iterator, multimap<int, string>::iterator> range; range = m.equal_range(2);
for (multimap<int, string>::iterator i = range.first; i != range.second; i++) { cout << i?>first << " ";
}
return 0;
}
The output will be:

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++; }
};
struct Odd { bool operator()(int v) { return v%2==0; } };
int main() {
vector<int> v1(10);
vector<int> v2(10);
generate(v1.begin(), v1.end(), Sequence(1));
stable_partition(v1.begin(),v1.end(), Odd());
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 <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));
reverse_copy(v1.begin(),v1.end(), v2.rbegin());
sort(v2.begin(), v2.end(), less_equal<int>());
for_each(v2.begin(), v2.end(), Out<int>(cout) );cout<<endl;
return 0;
}
Program outputs:

What happens when you attempt to compile and run the following code?
#include <iostream>
using namespace std;
int main()
{
cout<<true<<" "<<boolalpha<<false;
return 0;
}
Program outputs:

What happens when you attempt to compile and run the following code?
#include <deque>
#include <iostream>
#include <algorithm>
#include <set>
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};
int t1[]={1,2,3,4};
deque<int> d1(t, t+10);
set<int> s1(t, t+10);
sort(d1.begin(), d1.end());
cout<<includes(s1.begin(),s1.end(), t1,t1+4)<<" "<<includes(d1.begin(),d1.end(), t1,t1+4)
< <endl;
return 0;
}
Program outputs:

What happens when you attempt to compile and run the following code?
# include <iostream>
# include <algorithm>
#include <vector>
using namespace std;
int main () {
int t[] = {1,2,3,3,5,1,2,4,4,5};
vector<int> v (t,t+10);
vector<int>::iterator it = v.begin();
while ( (it = adjacent_find (it, v.end())) != v.end()) {
cout<<it?v.begin()<<" ";it++;
}
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);
generate(v1.rbegin(), v1.rend(), Sequence(1));
rotate(v1.begin(),v1.begin() + 1, 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 <vector>
#include <iostream>
int main ()
{
std::vector<int>v1;
for(int i = 0; i<10; i++) {v1.push_back(i); }
v1.resize(4);
std::vector<int>::iterator it = v1.end();
v1.insert(v1.end()?1, 4);
for(int i=0 ; i<= v1.size(); i++) {std::cout<<v1.at(i)+v1[i]<<" "; }std::cout<<std::endl; return 0;
}

What happens when you attempt to compile and run the following code?
# include <vector>
# include <iostream>
# include <algorithm>
using namespace std;
class B { int val;
public:
B(int v):val(v){}
int getV() const {return val;} bool operator < (const B & v) const { return val<v.val;} }; ostream & operator <<(ostream & out, const B & v) { out<<v.getV(); return out;} template<class T>struct Out {
ostream & out;
Out(ostream & o): out(o){}
void operator() (const T & val ) { out<<val<<" "; } };
int main() {
B t1[]={3,2,4,1,5};
B t2[]={5,6,8,2,1};
vector<B> v1(10,0);
sort(t1, t1+5);
sort(t2, t2+5);
set_symmetric_difference(t2,t2+5,t1,t1+5,v1.begin());
for_each(v1.begin(), v1.end(), Out<B>(cout));cout<<endl;
return 0;
}
Program outputs:

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;
}
int multiply (int a) {
return a*2;
}
int main() {
int t[] = { 10, 5, 9, 6, 2, 4, 7, 8, 3, 1 };
vector<int> v1(t, t+10);
set<int> s1(t, t+10);
transform(s1.begin(), s1.end(), v1.begin(), multiply);
transform(v1.begin(), v1.end(), s1.begin(), multiply);
for_each(s1.begin(), s1.end(), myfunction);
return 0;
}
Program outputs:

What happens when you attempt to compile and run the following code?
# include <iostream>
# include <set>
# include <vector>
using namespace std;
template<class T> void print(T start, T end) {
while (start != end) {
std::cout << *start << " "; start++;
}
}
int main(){
vector<int>v;
multiset<int> s;
for(int i=10; i>0; i??) {
v.push_back(i); s.push_back(i);
}
print(v.begin(), v.end()); print(s.begin(), s.end());cout<<endl;
return 0;
}

What happens when you attempt to compile and run the following code?
#include <iostream>
#include <algorithm>
#include <map>
using namespace std;
int main() {
int t[] = { 10, 5, 9, 6, 2, 4, 7, 8, 3, 1 };
map<int, int> m;
for(int i=0; i < 10; i++) {
m[i]=t[i];
}
pair<const int,int> p(5,5);
map<int, int>::iterator it = find(m.begin(), m.end(), p);
if (it != m.end())
{
cout<<it?>first<<endl;
}
else
{
cout<<"Not found!\n";
}
return 0;
}
Program outputs:

What happens when you attempt to compile and run the following code?
# include <deque>
# include <iostream>
# include <algorithm>
# include <functional>
using namespace std;
class B { int val;
public:
B(int v):val(v){}
int getV() const {return val;} bool operator > (const B & v) const { return val>v.val;} }; ostream & operator <<(ostream & out, const B & v) { out<<v.getV(); return out;} template<class T>struct Out {
ostream & out; Out(ostream & o): out(o){}
void operator() (const T & val ) { out<<val<<" "; } };
int main() {
int t[]={20, 30, 10, 20, 30, 10, 20, 30, 10, 20};
deque<B> d1(t, t+10);
sort(d1.begin(), d1.end(), greater<B>());
pair<deque<B> ::iterator, deque<B>::iterator > result = equal_range(d1.begin(), d1.end(),
B(20), greater<B>());
for_each(result.first, result.second, Out<B>(cout));cout<<endl;
return 0;
}
Program outputs:

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<<" "; } };
int main() {
int t[]={3,2,4,1,5,10,9,7,8,6};
vector<int> v1(t,t+10);
sort(v1.begin(), v1.end(), greater<int>());
cout<<min_element(v1.begin(), v1.end());
return 0;
}
Program outputs:

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"}; multimap<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 (multimap<int, string>::iterator i = m.begin(); i != m.end(); i++) { cout << i?>first << " ";
}
return 0;
}

弊社を連絡する

我々は12時間以内ですべてのお問い合わせを答えます。

オンラインサポート時間:( UTC+9 ) 9:00-24:00
月曜日から土曜日まで

サポート:現在連絡