下面这个声明会(D)
const std::string str1;
A:、导致编译错误
B、强制程序员在str1被使用前为其赋值
C、由具体实现决定其行为
D、将str1的内容置为空串。
string str1("haha");
cout << str1.size() << " " << str1.length() << " " << str1.capacity() << endl;
输出为4 4 15
string类型,size()等同于length()
输出string
string str("hello");
cout << "[cout]str is: " << str<< endl;
printf("[printf]str is: %s\n", str.c_str()); //c_str() is c format string
substr
string substr (size_t pos = 0, size_t len = npos) const;
产生子串
返回一个新建的 初始化为string对象的子串的拷贝string对象。
子串是,在字符位置pos开始,跨越len个字符(或直到字符串的结尾,以先到者为准)对象的部分。
https://blog.csdn.net/sunshihua12829/article/details/50484966
// string::substr
int main ()
{
std::string str="We think in generalities, but we live in details.";
// (quoting Alfred N. Whitehead)
std::string str2 = str.substr (3,5); // "think"
std::size_t pos = str.find("live"); // position of "live" in str
std::string str3 = str.substr (pos); // get from "live" to the end
std::cout << str2 << ' ' << str3 << '\n'; // think live in details.
return 0;
}
istringstream ostringstream stringstream
https://blog.csdn.net/xw20084898/article/details/21939811
int main()
{
/*
//istringstream
//istringstream istr; istr.str("1 56.7");
istringstream istr("1 56.7"); //上述两个过程可以简单写成
cout << istr.str() << endl;
int a;
float b;
istr >> a;
cout << a << endl;
istr >> b;
cout << b << endl;
//*/
/*
//ostringstream
ostringstream ostr1; // 构造方式1
ostringstream ostr2("abc"); // 构造方式2
// 方法str()将缓冲区的内容复制到一个string对象中,并返回
ostr1 << "ostr1 " << 2012 << endl; // 格式化,此处endl也将格式化进ostr1中
cout << ostr1.str();
// 建议:在用put()方法时,先查看当前put pointer的值,防止误写
long curPos = ostr2.tellp(); //返回当前插入的索引位置(即put pointer的值),从0开始
cout << "curPos = " << curPos << endl;
ostr2.seekp(2); // 手动设置put pointer的值
ostr2.put('g'); // 在put pointer的位置上写入'g',并将put pointer指向下一个字符位置
cout << ostr2.str() << endl;
//*** 重复使用同一个ostringstream对象时,建议:
//*** 1:调用clear()清除当前错误控制状态,其原型为 void clear (iostate state=goodbit);
//*** 2:调用str("")将缓冲区清零,清除脏数据
ostr2.clear();
ostr2.str("");
cout << ostr2.str() << endl;
ostr2.str("_def");
cout << ostr2.str() << endl;
ostr2 << "gggghh"; // 覆盖原有的数据,并自动增加缓冲区
cout << ostr2.str() << endl;
ostr2.str(""); // 若不加这句则运行时错误,因为_df所用空间小于gggghh,导致读取脏数据
ostr2.str("_df");
cout << ostr2.str() << endl;
// 输出随机内存值,危险
const char* buf = ostr2.str().c_str();
cout << buf << endl;
// 正确输出_df
string ss = ostr2.str();
const char *buffer = ss.c_str();
cout << buffer << endl;
//*/
//stringstream
/*
stringstream ss1("hello world");
string s1;
while (ss1 >> s1) {
cout << s1 << endl;
}
//*/
//*
stringstream ss2;
ss2<<"hello"<<"world"<<"china";
// str() method convert stringstream type to string type
cout << ss2.str() << endl; // helloworldchina
ss2 << "first";
cout << ss2.str() << endl; // helloworldchinafirst
//*/
//*
stringstream ss3("hello world china"); // it seems it restricts size
cout << ss3.str() << endl; // hello world china
ss3 << "first2";
cout << ss3.str() << endl; // first2world china
//*/
stringstream ss4("hello world china");
ss4.str(""); // empty ss4
ss4 << "first2";
cout << ss4.str() << endl; // first2
stringstream s5;
int first, second;
// 插入字符串
s5 << "456 ";
s5 << "23 ";
s5 << "02 ";
int i5;
while (s5 >> i5) {
cout << i5 << endl;
}
s5.clear();
s5 << true;
bool b1;
while (s5 >> b1) {
cout << b1 << endl;
}
cout << s5.str() << endl; // first2
return 0;
}