C++ 03 标准库类型

命名空间的using声明

#include <iostream>
#include <string>

using std::cin;
using std::cout;
using std::endl;
using std::string;
int main()
{
	string s;
	cin >> s;
	cout << s << endl;
	return 0;
}

string对象

string输入输出

#include <iostream>
#include <string>

using std::cin;
using std::cout;
using std::endl;
using std::string;
using std::getline;
int main()
{
	// string 对象初始化
	string s1;
	string s2(s1);
	string s3("value");
	string s4(3, 'c');  // "ccc"
//	while (cin >> s1) cout << s1 << endl;
	// 使用getline() 读取整行
	string line;
	while (getline(cin, line))
		cout << line << endl;
	return 0;
}

string对象操作

#include <iostream>
#include <string>

using std::cin;
using std::cout;
using std::endl;
using std::string;
using std::getline;
int main()
{
	string st("Hi boy");
	// size() 
	cout << "The size of " << st << "is " << st.size()
		<< " characters , including the newline" << endl;
	// empty()
	if (!st.empty())
		cout << "string " << st << " is not empty" << endl;
	// == < > <= >=
	if (st == "Hi boy")
		cout << "Two word is same" << endl;
	// + 
	string s3 = st + " Hi girl";
	cout << "string + is " << s3 << endl;
	// string中字符
	for (string::size_type ix = 0; ix != st.size(); ++ix)
		st[ix] = '*';
	cout << "usage of string::size_type , st is " << st << endl;

	return 0;
}

字符处理操作 cctype

cctype

vector 容器类型

#include <iostream>
#include <string>
#include <vector>

using std::cin;
using std::cout;
using std::endl;
using std::string;
using std::vector;

int main()
{
	// vector 初始化
	vector<int> vals(10); // 10 个0 
	string word;
	vector<string> text;
	cin >> word;
	text.push_back(word); // 添加元素
	for (vector<string>::size_type ix = 0; ix != text.size(); ++ix) // 遍历
		cout << ix << ":" << text[ix] << endl;
	// 迭代器
	for (vector<string>::iterator iter = text.begin(); iter != text.end(); iter++)
		*iter = "x";
	// 不可修改内容的迭代器
	for (vector<string>::const_iterator iter = text.begin(); iter != text.end(); iter++)
		cout <<  *iter << endl;
	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()
{
	// vector 初始化
	bitset<32> bitvec; // 32bits , all zero
	bitset<16> bitvec1(0xffff); //0...15 are set to 1 一个16进制数,
	string strval("1100");
	bitset<32> bitvec4(strval); // 1100
	string str("11111110000000011");
	bitset<32> bitvec5(str, 5, 4); // 4 bits start str[5] , 1100
	// bitset对象操作 
	bool is_set = bitvec.any(); // false
	bool is_not_set = bitvec.none(); // true 

	size_t size = bitvec.size(); // 32
	for (int index = 0; index != size; index += 2)
		bitvec.set(index); // bitvec[index] = 1;

	if (bitvec.test(0));
	if (bitvec[0]);

	bitvec.reset(); // all to 0
	bitvec.set();	// all to 1

	bitvec.flip(0);// 反转第一位
	bitvec[0].flip();
	bitvec.flip(); // 反转全部

	bitset<128> bitvec3(0xffff);
	unsigned long ulong = bitvec3.to_ullong(); // 取值与 二进制位输出 
	cout << "bitvec3 = " << ulong << " = " << bitvec3 << endl;
	return 0;
}
原文地址:https://www.cnblogs.com/hiqianqian/p/6863435.html