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>
using namespace std;
int main () {
int t[] = {1,2,3,4,5,1,2,3,5,4};
vector<int> v (t,t+10);
vector<int>::iterator it;
int m1[] = {1, 3, 2};
it = find_end (v.begin(), v.end(), m1, m1+3);
if (it != v.end())
cout << "Found at position: " << it?v.begin() << endl;
return 0;
}
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
int main () {
int t[] = {1,2,3,4,5,1,2,3,5,4};
vector<int> v (t,t+10);
vector<int>::iterator it;
int m1[] = {1, 3, 2};
it = find_end (v.begin(), v.end(), m1, m1+3);
if (it != v.end())
cout << "Found at position: " << it?v.begin() << endl;
return 0;
}
正解:B
解答を投票する
What happens when you attempt to compile and run the following code?
#include <iostream>
using namespace std;
template <typename T>
class A {
T_v;
public:
A() {}
A(T v): _v(v){}
T getV() { return _v; }
void add(T a) { _v+=a; }
template <class U>
U get(U a) {
return (U)(_v);
}
};
int main()
{
A<int> a(1);
a.add(10);
cout.setf( ios::showpoint);
cout << a.getV() << " " << a.get(1.0)<<endl;
return 0;
}
#include <iostream>
using namespace std;
template <typename T>
class A {
T_v;
public:
A() {}
A(T v): _v(v){}
T getV() { return _v; }
void add(T a) { _v+=a; }
template <class U>
U get(U a) {
return (U)(_v);
}
};
int main()
{
A<int> a(1);
a.add(10);
cout.setf( ios::showpoint);
cout << a.getV() << " " << a.get(1.0)<<endl;
return 0;
}
正解:A
解答を投票する
What happens when you attempt to compile and run the following code?
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
class A {
int a;
public:
A(int a) : a(a) {}
int getA() const { return a; } void setA(int a) { this?>a = a; }
bool operator==(A & b) { return a == b.a; }
};
struct Compare{
bool operator()(const A & a, const A & b) {return a.getA()==b.getA();};
};
int main () {
int t[] = {1,2,3,4,5,1,2,3,4,5};
vector<A> v (t,t+10);
vector<A>::iterator it;
A m1[] = {A(1), A(2), A(3)};
it = search (v.begin(), v.end(), m1, m1+3, Compare());
cout << "First found at position: " << it?v.begin() << endl;
return 0;
}
Program outputs:
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
class A {
int a;
public:
A(int a) : a(a) {}
int getA() const { return a; } void setA(int a) { this?>a = a; }
bool operator==(A & b) { return a == b.a; }
};
struct Compare{
bool operator()(const A & a, const A & b) {return a.getA()==b.getA();};
};
int main () {
int t[] = {1,2,3,4,5,1,2,3,4,5};
vector<A> v (t,t+10);
vector<A>::iterator it;
A m1[] = {A(1), A(2), A(3)};
it = search (v.begin(), v.end(), m1, m1+3, Compare());
cout << "First found at position: " << it?v.begin() << endl;
return 0;
}
Program outputs:
正解:E
解答を投票する
What happens when you attempt to compile and run the following code?
#include <deque>
#include <vector>
#include <iostream>
using namespace std;
int main ()
{
int t[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
vector<int> v1(t, t + 10);
deque<int> d1(v1.begin(), v1.end());
deque<int> d2;
d2 = d1;
d2.insert(d1.rbegin(), 10);
for(int i = 0; i<d1.size(); i++)
{
cout<<d1[i]<<" ";
}
return 0;
}
#include <deque>
#include <vector>
#include <iostream>
using namespace std;
int main ()
{
int t[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
vector<int> v1(t, t + 10);
deque<int> d1(v1.begin(), v1.end());
deque<int> d2;
d2 = d1;
d2.insert(d1.rbegin(), 10);
for(int i = 0; i<d1.size(); i++)
{
cout<<d1[i]<<" ";
}
return 0;
}
正解:A
解答を投票する
Which are NOT valid instantiations of priority_queue object:
# include <iostream>
# include <deque>
# include <list>
# include <queue>
# include <vector>
using namespace std;
int main()
{
deque<int> mydeck;list<int> mylist; vector<int> myvector;
priority_queue<int> first;//line I
priority_queue<int, deque<int> > second;//line II
priority_queue<int> third(first);//line III
priority_queue<int, list<int> > fourth(third);//line IV
priority_queue<int, vector<int> > fifth(myvector.begin(), myvector.end());//line V return 0;
}
# include <iostream>
# include <deque>
# include <list>
# include <queue>
# include <vector>
using namespace std;
int main()
{
deque<int> mydeck;list<int> mylist; vector<int> myvector;
priority_queue<int> first;//line I
priority_queue<int, deque<int> > second;//line II
priority_queue<int> third(first);//line III
priority_queue<int, list<int> > fourth(third);//line IV
priority_queue<int, vector<int> > fifth(myvector.begin(), myvector.end());//line V return 0;
}
正解:E
解答を投票する
What happens when you attempt to compile and run the following code?
#include <iostream>
#include <algorithm>
#include <set>
using namespace std;
struct Even {
bool operator ()(int a) {
return (a % 2)==0?true:false;
}
};
int main () {
int t[] = {1,2,3,2,3,5,1,2,7,3,2,1,10, 4,4,5};
set<int> s(t,t+15);
int number = count_if(s.begin(), s.end(), Even());
cout<< number<<endl;
return 0;
}
Program outputs:
#include <iostream>
#include <algorithm>
#include <set>
using namespace std;
struct Even {
bool operator ()(int a) {
return (a % 2)==0?true:false;
}
};
int main () {
int t[] = {1,2,3,2,3,5,1,2,7,3,2,1,10, 4,4,5};
set<int> s(t,t+15);
int number = count_if(s.begin(), s.end(), Even());
cout<< number<<endl;
return 0;
}
Program outputs:
正解:B
解答を投票する
What happens when you attempt to compile and run the following code?
# include <iostream>
# include <string>
using namespace std;
template <class T>
class A {
T_v;
public:
A() {}
A(T v): _v(v){}
T getV() { return _v; }
void add(T & a) { _v+=a; }
void add(string & a) {
_v.insert(0, a);
}
};
int main()
{
A<string>a("Hello");
string s(" world!");
a.add(s);
cout << a.getV() <<endl;
return 0;
}
# include <iostream>
# include <string>
using namespace std;
template <class T>
class A {
T_v;
public:
A() {}
A(T v): _v(v){}
T getV() { return _v; }
void add(T & a) { _v+=a; }
void add(string & a) {
_v.insert(0, a);
}
};
int main()
{
A<string>a("Hello");
string s(" world!");
a.add(s);
cout << a.getV() <<endl;
return 0;
}
正解:B
解答を投票する
What happens when you attempt to compile and run the following code?
# include <iostream>
# include <set>
# include <vector>
using namespace std;
int main(){
int t[] ={ 3, 4, 2, 1, 6, 5, 7, 9, 8, 0 };
vector<int>v(t, t+10);
multiset<int> s1(v.begin(),v.end());
s1.insert(v.begin(),v.end());
pair<multiset<int>::iterator,multiset<int>::iterator> range;
range = s1.equal_range(6);
while (range.first != range.second) {
cout<<*range.first<<" "; range.first++;
}
return 0;
}
# include <iostream>
# include <set>
# include <vector>
using namespace std;
int main(){
int t[] ={ 3, 4, 2, 1, 6, 5, 7, 9, 8, 0 };
vector<int>v(t, t+10);
multiset<int> s1(v.begin(),v.end());
s1.insert(v.begin(),v.end());
pair<multiset<int>::iterator,multiset<int>::iterator> range;
range = s1.equal_range(6);
while (range.first != range.second) {
cout<<*range.first<<" "; range.first++;
}
return 0;
}
正解:E
解答を投票する
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 t1[]={3,2,4,1,5};
int t2[]={5,6,8,2,1};
vector<int> v1(10);
sort(t1, t1+5);
sort(t2, t2+5);
set_intersection(t1,t1+5,t2,t2+5,v1.begin());
for_each(v1.begin(), v1.end(), Out<int>(cout));cout<<endl;
return 0;
}
Program outputs:
# 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 t1[]={3,2,4,1,5};
int t2[]={5,6,8,2,1};
vector<int> v1(10);
sort(t1, t1+5);
sort(t2, t2+5);
set_intersection(t1,t1+5,t2,t2+5,v1.begin());
for_each(v1.begin(), v1.end(), Out<int>(cout));cout<<endl;
return 0;
}
Program outputs:
正解:E
解答を投票する
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;} }; 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_difference(t1,t1+5,t2,t2+5,v1.begin());
for_each(v1.begin(), v1.end(), Out<B>(cout));cout<<endl;
return 0;
}
Program outputs:
# 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;} }; 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_difference(t1,t1+5,t2,t2+5,v1.begin());
for_each(v1.begin(), v1.end(), Out<B>(cout));cout<<endl;
return 0;
}
Program outputs:
正解:B
解答を投票する
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 t1[]={3,2,4,1,5};
int t2[]={6,10,8,7,9};
vector<int> v1(10);
sort(t1, t1+5); sort(t2, t2+5);
copy(t1,t1+5,v1.begin());
copy(t2,t2+5,v1.begin()+5);
merge(v1.begin(), v1.begin()+5,v1.end());
for_each(v1.begin(), v1.end(), Out<int>(cout));cout<<endl;
return 0;
}
Program outputs:
# 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 t1[]={3,2,4,1,5};
int t2[]={6,10,8,7,9};
vector<int> v1(10);
sort(t1, t1+5); sort(t2, t2+5);
copy(t1,t1+5,v1.begin());
copy(t2,t2+5,v1.begin()+5);
merge(v1.begin(), v1.begin()+5,v1.end());
for_each(v1.begin(), v1.end(), Out<int>(cout));cout<<endl;
return 0;
}
Program outputs:
正解:B
解答を投票する
Which changes introduced independently will allow the code to compile and display "one"
"eight" "nine" "ten"? Choose all that apply.
#include <iostream>
# include <map>
# include <string>
using namespace std;
class A {
int a;
public:
A(int a):a(a){}
int getA() const { return a;}
/* Insert Code Here 1 */
};
/* Insert Code Here 2 */
int main(){
int t[] ={ 3, 4, 2, 1, 6, 5, 7, 9, 8, 10 };
string s[] = {"three", "four", "two", "one", "six","five", "seven", "nine","eight","ten"}; multimap<A,string> m;/* Replace Code Here 3 */
for(int i=0; i<10; i++) {
m.insert(pair<A,string>(A(t[i]),s[i]));
}
m.erase(m.lower_bound(2),m.upper_bound(7));
multimap<A, string>::iterator i=m.begin();/* Replace Code Here 4 */
for( ; i!= m.end(); i++) {
cout<<i?>second<<" ";
}
cout<<endl;
return 0;
}
"eight" "nine" "ten"? Choose all that apply.
#include <iostream>
# include <map>
# include <string>
using namespace std;
class A {
int a;
public:
A(int a):a(a){}
int getA() const { return a;}
/* Insert Code Here 1 */
};
/* Insert Code Here 2 */
int main(){
int t[] ={ 3, 4, 2, 1, 6, 5, 7, 9, 8, 10 };
string s[] = {"three", "four", "two", "one", "six","five", "seven", "nine","eight","ten"}; multimap<A,string> m;/* Replace Code Here 3 */
for(int i=0; i<10; i++) {
m.insert(pair<A,string>(A(t[i]),s[i]));
}
m.erase(m.lower_bound(2),m.upper_bound(7));
multimap<A, string>::iterator i=m.begin();/* Replace Code Here 4 */
for( ; i!= m.end(); i++) {
cout<<i?>second<<" ";
}
cout<<endl;
return 0;
}
正解:B、C、D
解答を投票する
What happens when you attempt to compile and run the following code?
# include <iostream>
# include <algorithm>
#include <vector>
using namespace std;
bool compare(int a, int b) { return a == b; }
int main () {
int t[] = {1,2,3,4,5,1,2,3,4,5};
vector<int> v (t,t+10);
vector<int>::iterator it = v.begin();
int m1[] = {1, 2, 3};
while ( (it = find_first_of (it, v.end(), m1, m1+3)) != v.end()) {
cout<<it?v.begin()<<" ";
}
cout<< endl;
return 0;
}
# include <iostream>
# include <algorithm>
#include <vector>
using namespace std;
bool compare(int a, int b) { return a == b; }
int main () {
int t[] = {1,2,3,4,5,1,2,3,4,5};
vector<int> v (t,t+10);
vector<int>::iterator it = v.begin();
int m1[] = {1, 2, 3};
while ( (it = find_first_of (it, v.end(), m1, m1+3)) != v.end()) {
cout<<it?v.begin()<<" ";
}
cout<< endl;
return 0;
}
正解:E
解答を投票する
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 Add {
int operator()(int & a, int & b) {
return a+b;
}
};
int main() {
int t[]={1,2,3,4,5,6,7,8,9,10};
vector<int> v1(t, t+10);
vector<int> v2(10);
transform(v1.begin(), v1.end(), v2.begin(), bind1st(1,Add()));
for_each(v2.rbegin(), v2.rend(), Out<int>(cout));cout<<endl;
return 0;
}
Program outputs:
# 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 Add {
int operator()(int & a, int & b) {
return a+b;
}
};
int main() {
int t[]={1,2,3,4,5,6,7,8,9,10};
vector<int> v1(t, t+10);
vector<int> v2(10);
transform(v1.begin(), v1.end(), v2.begin(), bind1st(1,Add()));
for_each(v2.rbegin(), v2.rend(), Out<int>(cout));cout<<endl;
return 0;
}
Program outputs:
正解:B
解答を投票する
Which method added to class B at the marked spot will allow the code below to compile?
Choose all possible solutions.
# include <vector>
# include <iostream>
# include <algorithm>
using namespace std;
class B { int val;
public:
B(int v):val(v){}
int getV() const {return val;}
/* Insert Code Here */
};
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[]={8, 10, 5, 1, 4, 6, 2, 7, 9, 3};
vector<B> v1(t, t+10);
sort(v1.begin(), v1.end(), greater<B>());
for_each(v1.begin(), v1.end(), Out<B>(cout));cout<<endl;
return 0;
}
Choose all possible solutions.
# include <vector>
# include <iostream>
# include <algorithm>
using namespace std;
class B { int val;
public:
B(int v):val(v){}
int getV() const {return val;}
/* Insert Code Here */
};
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[]={8, 10, 5, 1, 4, 6, 2, 7, 9, 3};
vector<B> v1(t, t+10);
sort(v1.begin(), v1.end(), greater<B>());
for_each(v1.begin(), v1.end(), Out<B>(cout));cout<<endl;
return 0;
}
正解:B、C、E
解答を投票する
What happens when you attempt to compile and run the following code?
# include <iostream>
# include <set>
# include <list>
using namespace std;
int main(){
int t[] ={ 1, 1, 2, 2, 3, 3, 4, 4, 5, 5 };
list<int>v(t, t+10);
multiset<int> s1(v.begin(),v.end());
if (s1.count(3) == 2) {
s1.erase(3);
}
for(multiset<int>::iterator i=s1.begin();i!= s1.end(); i++) {
cout<<*i<<" ";
}
return 0;
}
# include <iostream>
# include <set>
# include <list>
using namespace std;
int main(){
int t[] ={ 1, 1, 2, 2, 3, 3, 4, 4, 5, 5 };
list<int>v(t, t+10);
multiset<int> s1(v.begin(),v.end());
if (s1.count(3) == 2) {
s1.erase(3);
}
for(multiset<int>::iterator i=s1.begin();i!= s1.end(); i++) {
cout<<*i<<" ";
}
return 0;
}
正解:C
解答を投票する