尼玛。。。第二章,忒简单了。。。
过了一遍,直接Pass 算了
使用using 声明
#include <iostream>
#include <string>
using std::cin;
using std::cout;
using std::endl;
using std::string;
int main()
{
string code = "This is Test Code";
cout << code
<< endl;
system("pause");
return 0;
}
结果如下:
getline 函数
#include <iostream>
#include <string>
using std::cin;
using std::cout;
using std::endl;
using std::string;
int main()
{
string code;
while(getline(cin, code)){
cout << code << endl;
}
system("pause");
return 0;
}
标准库的 string 类型的成员方法, size() 的返回值,竟然不是 int 类型,而是,神奇的 string::size_type 类型。擦。。。不管是啥玩意儿,总之,不能赋值给 int 变量!
逐个显示字符串中的 字符
#include <iostream>
#include <string>
using std::cin;
using std::cout;
using std::endl;
using std::string;
int main()
{
string str = "string testing";
for(string::size_type index = 0; index != str.size(); index++)
cout << str[index] << endl;
system("pause");
return 0;
}
vector 用法,
必须引入 #include <vector>
#include <iostream>
#include <string>
#include <vector>
using std::cin;
using std::cout;
using std::endl;
using std::string;
using std::vector;
int main()
{
string word;
vector<string> text;
while(cin >> word){
text.push_back(word);
}
for(vector<string>::size_type index = 0; text.size() != index; ++index)
{
text[index] = "null";
cout << index << ":" << text[index] << endl;
}
system("pause");
return 0;
}
迭代器,所有容器都定义了自己的迭代器,但是,并不是所有的容器都支持下标操作符
解引用操作符 *
#include <iostream>
#include <string>
#include <vector>
using std::cin;
using std::cout;
using std::endl;
using std::string;
using std::vector;
int main()
{
string word;
vector<string> text;
text.push_back("first");
text.push_back("second");
text.push_back("third");
for(vector<string>::iterator iter = text.begin();
iter != text.end(); ++iter)
{
*iter = "string";
cout << *iter << endl;
}
system("pause");
return 0;
}
bitset
#include <iostream>
#include <string>
#include <bitset>
using std::cin;
using std::cout;
using std::endl;
using std::string;
using std::bitset;
int main()
{
string word("11111111");
bitset<32> bit(word);
unsigned long u_long;
u_long = bit.to_ulong();
cout << "unsigned long number is: " << u_long << endl;
cout << "bit :" << bit << endl;
system("pause");
return 0;
}