C++ Primer 学习笔记 - 第三章

尼玛。。。第二章,忒简单了。。。

过了一遍,直接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;
}


结果如下:



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值