分享

C++ Primer第三章字符串,向量和数组习题

 dongtongtong 2016-03-06
第三章字符串,向量和数组习题
3.1
3.2
//读取一行
int main()
{
string line = "";
getline(cin,line);
cout << line << endl;
return 0;
}
int main()
{
string line = "";
while( cin >> line)
{
cout << line << endl;
}
return 0;
}
3.3
string 类型的输入操作符对空白字符的处理:读取并忽略有效字符(非空白字
符)之前所有的空白字符,然后读取字符直至再次遇到空白字符,读取终止(该空白字符仍留在输入流中)。
getline函数对空白字符的处理:不忽略行开头的空白字符,读取字符直至遇到
换行符,读取终止并丢弃换行符(换行符从输入流中去掉但并不存储在string
对象中)。
3.4
int main()
{
string str1,str2;
cin >> str1 >> str2;
if(str1 != str2)
{
cout << (str1 > str2 ?str1:str2) << endl;
}
return 0
}
int main()
{
string str1,str2;
cin >> str1 >> str2;
if(str1.size() != str2.size())
{
cout << (str1.size() > str2.szie() ?str1:str2) << endl;
}
return 0
}
3.5
int _tmain(int argc, _TCHAR* argv[])
{
string result,temp;
while (cin >> temp)
{
result += temp + " ";
}
cout << result << endl;
return 0;
}
3.6
string str = "hello,world";
for (auto &c : str)
c = 'X';
3.7
可以直接将c声明为char&
3.8
for(decltype(str.size()) i = 0; i < str.size(); i++)
str[i] = 'X';
3.9
不合法因为s是一个空字符串输出0错误
3.10
int main()
{
string str;
cin >> str >> endl;
for (auto &c : str)
{
if (!ispunct(c))
cout << c;
}
cout << endl;
return 0;
}
3.11
不合法
3.12
b不合法不同类型不可以赋值
3.13
a 没有元素
b 10个元素
c 10个42
d 1个10
e 两个10,42
f 10个
g 10个“hi”
3.14
int main()
{
vector<int> ivec;
int temp;
while (cin >> temp)
{
ivec.push_back(temp);
}
return 0;
}
3.15略
3.16略
3.17
vector<string>strVec;
string temp;
while (cin >> temp)
{
strVec.push_back(temp);
}
for (auto & str : strVec)
{
cout << upper(str) << endl;
}
3.18
不合法 vector大小为0
3.19
vector ivec(10,42);
vector ivec{42,42,....};
vector ivec = {42,42,...};
3.20
vector<int>ivec;
int i;
while (cin >> i)
{
ivec.push_back(i);
}
for (decltype(ivect.size())index = 0; index < ivect.size() -1; index += 2)
{
cout << ivec[index] + ivec[index +1] << endl;
}
if(ivec.size() % 2 != 0)
cout << "The last element no sum" << endl;

decltype(ivec.size()) first,last = ivec.size()
for(;first < last; fisrt ++,last--)
cout << ivec[first] + ivec[lasr] << endl;

3.21
for (auto it = ivec.begin(); it != ivec.end(); ++ it)
cout << *it <<endl;
3.22
3.23
vector<int>ivec = {0,1,2,3,4,5,6,7,8,9};
for (auto it = ivec.begin();it != ivec.end(); += ite)
{
*it = (*it) * 2;
}
3.24
3.25
int main()
{
vector<unsigned>scores(11,0);
unsigned grade;
decltype(scores.size()) n = 0;
while (cin >> grade)
{
n = grade /10;
auto it = scores.begin();
it = it +n;
(*it)++;
}
for (auto itr : scores)
{
cout << itr << "";
}

return 0;
}
3.26
用第二种方法beg和end都是指针或迭代器的话无法通过编译
因为指针和迭代器运算不支持相加运算却支持相减运算
3.27 
a)buffer_size不是常量值
c)错误txt_size不能替代常量表达式
d)错误 数组个数不对
3.28
sa,sa2都是空字符串
ia是0,ia2是未定义
3.29
数组大小是固定的,下标操作没有进行越界检查
不提供size(),push_back()等操作接口
3.30
constexpr size_t array_size = 10;
int ia[array_size];
for (size_t ix = 0; ix < array_size; ++ix)
{
ia[ix] = ix;
}
3.31
int array[] = {0,1,2,3,4,5,6,7,8,9};
3.32
int copy_array[10];
for (int i = 0; i < 10; i++)
{
copy_array[i] = array[i];
}
3.33
scores未初始化内容是未知的
3.34
让p1指向p2所指向的元素
参与运算的两个指针必须指向同一个数组才是合法的行为
3.35
int arr[10];
for (auto p = begin(arr); p != end(arr); p++)
{
*p = 0;
}
3.36
bool IsEqualArray(int *arr1,int n1,int *arr2,int n2)
{
if (n1 != n2) return false;
for (int i = 0; i < n1; i++)
{
if (arr1[i] != arr2[i]) return false;
}
return true;
}
3.37
输出
h
e
l
l
o
3.38 指针相减表示指针的距离但是相加没有什么意义
3.39
string对象可以直接比较
C风格的用strcmp比较
3.40
char arr1[] = "hello,world.";
char arr2[] = "I am in beijing";
char arr3[30];
strcpy_s(arr3,arr1);
strcat_s(arr3,arr2);
cout << arr3 << endl;
3.41
int arr[] = {0,1,2};
std::vector<int> vec(begin(arr),end(arr));
3.42
vector<int>ivec = {1,2,3};
int *a = new int[ivec.size()];
int idx = 0;
for (auto &i : ivec)
{
*(a+idx) = i;
idx++;
}
3.43
int ia[] = {0,1,2};
for (int &i : ia)
{
cout << i << endl;
}
for (int i = 0; i < 3; i ++)
{
cout << ia[i] << endl;
}
for (int *a = begin(ia); a != end(ia); a++)
{
cout << *a << endl;
}
3.44
3.45



    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多