2_2_7 string、vector、iterator、类型转换

string

#include "hjcommon.hpp"

using namespace std;

HJ_NS_USING

int main_2_2_7(int argc, char *argv[])
{
	// std::string
	String s1 = "some"; // std::string 可变长字符串
	int sof = sizeof s1; // 32
	int size = s1.size(); // 返回字节个数,linux中用的utf8一个汉字占三字节,length()函数与size()同义
	cout << sof << ", " << s1.size() << endl; // 32, 4

	char str[] = "nihao";
	char *ps = "some"; // 等同于 const char *ps = "some";
	str[0] = '2'; // 可以
//	ps[0] = '2'; // 报错
	cout << str << "," << ps << endl; // 2ihao,some
	String s2 = "nihao";
	s2[0] = '2'; // 可以
	cout << s2 << endl; // 2ihao

	// empty() == != + c_str()
	
	cout << "-----------------------------------------------------" << endl;
	// 整数与字符串互转 ,需要 #include <sstream>
	string sa = static_cast<ostringstream*>(&(ostringstream() << dec << 0x0a))->str(); // 10, 将十六进制的 0x0a 转换为十进制的字符串
	cout << sa << endl;
	cout