3.1
#include <iostream>
using std::cin;
using std::cout;
using std::endl;
int main() {
double base;
int exponent;
cin >> base >> exponent;
double sum(1.0);
for(int i=1; i<=exponent; i++)
sum *= base;
cout << sum << endl;
return 0;
}
3.2
所谓默认构造函数是指,当没有初始化值作为参数的时候,对象调研的构造函数。
3.3
#include <iostream>
#include <string>
using std::cin;
using std::cout;
using std::endl;
using std::string;
int main() {
string s1; // 默认构造函数
string s2("niubi"); // 字符串字面值构造
string s3(s2); // 另一个string对象构造
string s4(10, 'f'); // 用字符构造,第一个参数是重复的次数
cout << s1 << endl;
cout << s2 << endl;
cout << s3 << endl;
cout << s4 << endl;
return 0;
}
3.4
都是空字符串。string是类类型,都会调用默认构造函数。这和内置类型的初始化行为不同。
3.5
每次读取一行文本
#include <iostream>
#include <string>
using std::cin;
using std::cout;
using std::endl;
using std::string;
int main() {
string s;
while (getline(cin, s))
cout << s << endl;
return 0;
}
每次读取一个单词
#include <iostream>
#include <string>
using std::cin;
using std::cout;
using std::endl;
using std::string;
int main() {
string s;
while (cin >> s)
cout << s << endl;
return 0;
}
3.6
string类型的>>操作符,只取第一个非空格字符开始到第一个空格字符为止。
getline截取一行(不包括回车换行符),不删除行首的空格字符。
3.7
#include <stdio.h>
#include <iostream>
#include <string>
#include <cctype>
#include <ctype.h>
using namespace std;
int main() {
string a, b;
cin >> a >> b;
if(a == b)
printf("equal\n");
else {
printf("not equal, %s is bigger\n", a>b?a.c_str():b.c_str());
}
if(a.size() == b.size())
printf("size equal\n");
else
printf("size, %s is longer\n", a.size()>b.size()?a.c_str():b.c_str());
return 0;
}
3.8
ctrl+d,或者ctrl+Z都不能退出循环,这是咋回事?
#include <iostream>
using namespace std;
int main() {
string str, total;
while (cin >> str) {
if(str == "quit")
break;
total += str;
}
cout << total << endl;
return 0;
}
以空格隔开
#include <iostream>
using namespace std;
int main() {
string str, total;
while (cin >> str) {
if(str == "quit")
break;
total += str + " ";
}
cout << total << endl;
return 0;
}
3.9
想输出字符串的第一个字符,但是字符串为空,所以下标访问溢出,非法。
3.10
#include <iostream>
using namespace std;
int main() {
string str, result;
cin >> str;
for(string::size_type i = 0; i<str.size(); i++) {
if (ispunct(str[i]) == 0) {
// result[result.size()] = str[i]; // 这个语句为什么不起效果?因为用下标进行赋值,不能增加任何元素,仅能对已经存在的元素进行赋值,否则无响应。
result.append(1, str[i]);
}
}
cout << "result:" <<result << endl;
return 0;
}
3.11
(b)定义不正确。ivec是vector的vector,不能初始化元素类型是string的vector。
3.12
(a) 元素的个数为0,
(b) 元素的个数为10,内置类型,都初始化为0
(c) 元素的个数为10,值是42
(d) 元素的个数为0
(e) 元素的个数为10,类类型,调用默认构造函数,都是空字符串
(f) 元素的个数为10,都是hello的string。
3.13
#include <iostream>
#include <vector>
using namespace std;
int main() {
int val;
vector<int> data;
while (cin >> val)
data.push_back(val);
for(vector<int>::size_type i=0; i<data.size()-1; i+=2)
cout << data[i] + data[i+1] << endl;
printf("%s\n", data.size()%2==1?"last element without sum":"");
for(vector<int>::size_type i=0; i<data.size()/2; i++)
cout << data[i] + data[data.size()-i-1] << endl;
return 0;
}
3.14
#include <iostream>
#include <vector>
using namespace std;
int main() {
string a;
vector<string> b;
while (cin >> a) {
if(a == "quit")
break;
b.push_back(a);
}
for(vector<string>::size_type i=0; i<b.size(); i++)
for(string::size_type j=0; j<b[i].size(); j++)
b[i][j] = toupper(b[i][j]);
for(vector<string>::size_type i=0; i<b.size(); i++) {
printf("%s\t", b[i].c_str());
if(i%8==7)
printf("\n");
}
return 0;
}
3.15
不合法。由于ivec是个空的vector,这是赋值给一个不存在的元素,必定导致运行时错误。
改为ivec.push_back(42)。
3.16
一般有4中定义方式:
a 是先定义一个空vector,然后动态添加
b 是直接初始化为10个42
c 是从一个vector初始化
d 是先初始化有10个元素的vector,然后修改每个元素的值。
#include <iostream>
#include <vector>
using namespace std;
int main() {
vector<int> a;
for(int i=0; i<10; i++)
a.push_back(42);
vector<int> b(10, 42);
vector<int> c(a);
vector<int> d(10);
for(vector<int>::size_type i=0; i<d.size(); i++)
d[i] = 42;
return 0;
}
3.17
重写3.13的迭代器版本
#include <iostream>
#include <vector>
using namespace std;
int main() {
int val;
vector<int> data;
while (cin >> val)
data.push_back(val);
for(vector<int>::iterator iter=data.begin(); iter<data.end()-1; iter+=2)
cout << *iter + *(iter+1) << endl;
printf("%s\n", data.size()%2==1?"last element without sum":"");
for(vector<int>::const_iterator i=data.begin(), j=data.end()-1; i<j; i++, j--)
cout << *i + *j << endl;
return 0;
}
3.14的迭代器版本
#include <iostream>
#include <vector>
using namespace std;
int main() {
string a;
vector<string> b;
while (cin >> a) {
if(a == "quit")
break;
b.push_back(a);
}
for(vector<string>::iterator i=b.begin(); i!=b.end(); i++)
for(string::iterator j=i->begin(); j<i->end(); j++)
*j = toupper(*j);
int count(0);
for(vector<string>::const_iterator iter=b.begin(); iter!=b.end(); iter++) {
printf("%s\t", iter->c_str());
if(count++%8==7)
printf("\n");
}
return 0;
}
3.18
#include <iostream>
#include <vector>
using namespace std;
int main() {
vector<int> a;
for(int i=0; i<10; i++)
a.push_back(i);
for(vector<int>::iterator iter=a.begin(); iter!=a.end(); iter++)
*iter *= 2;
for(vector<int>::const_iterator iter=a.begin(); iter!=a.end(); iter++)
cout << *iter << endl;
return 0;
}
3.19
参见上题。
3.20
用了两种迭代器:
iterator 如果要修改元素,用这个迭代器
const_iterator 如果仅是遍历读取元素,用这个迭代器。
3.21
const迭代器没啥用,一旦定义之后,就不能指向其他元素。
const_iterator迭代器用于只读的遍历元素,其指向的值不能变,但迭代器本身是可以++,--操作的。
3.22
两个迭代器相加没有意义,这样会造成未定义的行为。
3.23
(a) 64位,值是32的位模式,也就是00100000,这是低8位,其余补0
(b) 32位,值是1010101的位模式,1010101被当做一个unsigned long,而不是二进制数,该输出是:00000000000011110110100110110101
(c) 从字符串获取的位模式,高低位顺序相反。字符串的高位是bitset的低位。
3.24
#include <iostream>
#include <vector>
#include <bitset>
using namespace std;
int main() {
vector<int> a;
a.push_back(1);
a.push_back(2);
a.push_back(3);
a.push_back(5);
a.push_back(8);
a.push_back(13);
a.push_back(21);
for(vector<int>::size_type i=0; i<a.size(); i++) {
bitset<8> tmp(a[i]);
cout << tmp << endl;
}
bitset<32> b;
for(vector<int>::const_iterator iter=a.begin(); iter!=a.end(); iter++) {
bitset<32> tmp(*iter);
for(size_t i=0; i<tmp.size(); i++)
if (tmp.test(i))
b.set(i);
}
cout << b << endl;
return 0;
}