c++primer4 Chapter3标准库类型3.1、3.2、3.3
3.1 命名空间的using声明
3.2标准库string类型
Exercise3.3
不带初始化式,使用默认构造函数初始化string对象;使用一个已存在的string对象作为初始化式,将新创建的string对象初始化为已存在string对象的副本;使用字符串字面值作为初始化式,将新创建的string对象初始化为字符串字面值的副本。
Exercise3.4
都是空字符串。
getline应用举例:
#include <iostream>
#include <string>
using namespace std;
int main ()
{
string name;
cout << "Please, enter your full name: ";
getline (cin,name);
cout << "Hello, " << name << "!\n";
return 0;
}
用下标操作符分别取出string对象的每个字符,分行输出:
#include <iostream>
#include <string>
using namespace std;
int main ()
{
string str("some string");
for (string::size_type ix=0;ix!=str.size();++ix)
cout << str[ix]<<endl;
return 0;
}
#include <iostream>
#include <string>
using namespace std;
int main ()
{
cout<<"Enter a string:";
string str;
cin>>str;
for (string::size_type ix=0;ix!=str.size();++ix)
cout << str[ix]<<endl;
return 0;
}
Exercise3.7
#include <iostream>
#include <string>
using namespace std;
int main ()
{
cout<<"Enter two strings:";
string str1,str2;
cin>>str1>>str2;
if (str1==str2)
cout<<"They are equal."<<endl;
else if (str1>str2)
cout<<str1<<" is big than "<<str2<<endl;
else
cout <<str2<<" is big than "<<str1<<endl;
return 0;
}
#include <iostream>
#include <string>
using namespace std;
int main ()
{
cout<<"Enter two strings:";
string str1,str2;
cin>>str1>>str2;
if (str1.size()==str2.size())
cout<<"They are equal."<<endl;
else if (str1.size()>str2.size())
cout<<str1<<" is longer than "<<str2<<endl;
else
cout <<str2<<" is longer than "<<str1<<endl;
return 0;
}
Exercise3.8
#include <iostream>
#include <string>
using namespace std;
int main()
{
string result_str, str;
cout << "Enter several strings (ctrl+z to end): " << endl;
while(cin>>str)
result_str += str;
cout << "The result is: " << endl
<< result_str << endl;
return 0;
}
注:在输入字符串完毕后,需在下一行按Ctrl+z,然后回车。
#include <iostream>
#include <string>
using namespace std;
int main()
{
string result_str, str;
cout << "Enter several strings (ctrl+z to end): " << endl;
while(cin>>str)
result_str +=" "+str;
cout << "The result is: " << endl
<< result_str << endl;
return 0;
}
Exercise3.9
该程序输出string对象s所对应字符串的第一个字符。
不合法,因为s是一个空字符串,长度为0,s[0]是无效的。
Exercise3.10
#include <iostream>
#include <string>
#include <cctype>
using namespace std;
int main()
{
string s1, result_str;
bool has_punct = false; // to mark if there is a punct
char ch;
cout << "Enter a string" << endl;
getline(cin, s1);
for (string::size_type index = 0; index != s1.size(); ++index)
{
ch = s1[index];
if(ispunct(ch))
has_punct = true;
else
result_str += ch;
}
if (has_punct)
cout << "The result is: " << result_str << endl;
else {
cout<<"No punction charactor in the string?!"<<endl;
return 1;
}
return 0;
}
3.3标准库vector类型
Exercise3.11
(b)不正确,因为容器中两者类型不同。
Exercise3.12
(a) 元素个数为0
(b) 10,均为0
(c) 10,均为42
(d) 0
(e) 10,均为空字符串
(f) 10,均为字符串"hello"
Exercise3.12
#include <iostream>
#include <vector>
using std::cin;
using std::cout;
using std::endl;
using std::vector;
int main()
{
vector<int> ivec;
int i;
cout << "Enter numbers(ctrl+z to end): " << endl;
while ( cin>>i )
ivec.push_back(i);
if (ivec.size() == 0)
{
cout << "No number..." << endl;
return -1;
}
cout << "Sum of each pair is as follows: " <<endl;
for (vector<int>::size_type ix = 0; ix != ivec.size()-1 ; ix = ix+2)
{
cout << ivec[ix] + ivec[ix+1] << "\t";
}
if (ivec.size() % 2 != 0) // the last elem is left
cout << endl
<< "The last elem is not be sumed" <<endl;
return 0;
}
#include <iostream>
#include <vector>
using std::cin;
using std::cout;
using std::endl;
using std::vector;
int main()
{
vector<int> ivec;
int i;
cout << "Enter numbers(ctrl+z to end): " << endl;
while ( cin>>i )
ivec.push_back(i);
if (ivec.size() == 0)
{
cout << "No number..." << endl;
return -1;
}
cout << "Sum is as follows: " <<endl;
vector<int>::size_type cnt = 0, first, last;
for (first = 0, last = ivec.size()-1;
first < last; ++first, --last)
{
cout << ivec[first] + ivec[last] << "\t";
}
if (first == last)
{
cout << "The centor elem hasn't been summed" << endl;
}
}
Exercise3.14
#include <iostream>
#include <string>
#include <vector>
#include <cctype>
using std::cin;
using std::cout;
using std::endl;
using std::vector;
using std::string;
int main()
{
vector<string> svec;
string str;
cout << " Enter text(ctrl+z to end): " << endl;
while (cin>>str)
svec.push_back(str);
if (svec.size() == 0)
{
cout << "No string" << endl;
return -1;
}
cout << "The transformed elem is as follows: " << endl;
for (vector<int>::size_type ix = 0; ix != svec.size(); ++ix)
{
for (string::size_type iy = 0; iy != svec[ix].size(); ++iy)
if (islower(svec[ix][iy]))
svec[ix][iy] = toupper(svec[ix][iy]);
cout << svec[ix] << " ";
if ((ix+1)%8 == 0)
cout << endl;
}
return 0;
}
Exercise3.15
不合法。因为初始时为空对象。ivec[0]不存在。简单来说即不能用c语言数组的思维。
更正:ivec.push_back(42);
Exercise3.15
vector<int> ivec(10,42)
vector<int> ivec(10);
for (int ix=0;ix<10;++ix)
ivec[ix]=42;
vector<int> ivec(10);
for (int cnt=1;cnt<=10;++cnt)
ivec.push_back(42);