C++ STL string的输出
生活随笔
收集整理的這篇文章主要介紹了
C++ STL string的输出
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
代碼如下:
#include <iostream> #include <string> //要與c語言里面的#include <string.h>區分 using namespace std;int main(){string str("abcdefg");cout << str << endl;cout << str.c_str() << endl; //這個c_str()只是new出15個字節放abcdefg而已。返回char*cout << str[0] << endl; //注意:此處不能越界,會崩潰!!!cout << str.at(1) << endl; //注意:此處越界是異常,不是崩潰!try{str.at(10);}catch (out_of_range a){cout << "越界了!" << endl;}system("pause");return 0; }運行結果如下:
總結
以上是生活随笔為你收集整理的C++ STL string的输出的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: C++获取虚表中的函数并调用
- 下一篇: C++工作笔记-对二级指针的进一步理解(