String类
2015-08-18 20:28
302 查看
在C++中,要使用 string 类,必须在程序中包含头文件string。string 类位于名称空间 std 中,因此必须提供一条 using 编译指令,或者使用 std::string 来引用它。
string类定义隐藏了字符串的数组性质,能够像处理普通变量那样处理字符串。通过以下程序可以说明 string 对象和字符数组之间的一些相同点和不同点。
例:
运行结果:
参考资料:
string类定义隐藏了字符串的数组性质,能够像处理普通变量那样处理字符串。通过以下程序可以说明 string 对象和字符数组之间的一些相同点和不同点。
例:
// strtype1.cpp -- using the C++ string class #include <iostream> #include <string> // make string class available int main() { using namespace std; char charr1[20]; // create an empty array char charr2[20] = "jaguar"; // create an initialized array string str1; // create an empty string object string str2 = "panther"; // create an initialized string cout << "Enter a kind of feline: "; cin >> charr1; cout << "Enter another kind of feline: "; cin >> str1; // use cin for input cout << "Here are some felines:\n"; cout << charr1 << " " << charr2 << " " << str1 << " " << str2 // use cout for output << endl; cout << "The third letter in " << charr2 << " is " << charr2[2] << endl; cout << "The third letter in " << str2 << " is " << str2[2] << endl; // use array notation return 0; }
运行结果:
参考资料:
《 C++ Primer Plus (第6版)中文版 》P82-83
相关文章推荐
- Codeforces Gym 100650C The Game of Efil 模拟+阅读题
- Python笔记一
- poj 2135最小费用最大流
- DOM——JavaScript高级程序设计笔记(8)
- 织梦 channel 标签 指定typeid时 currentstyle无效的修复办法
- 神奇的table-cell
- 面试常见问题分析
- 源代码解读Cas实现单点登出(single sign out)功能实现原理
- nginx学习笔记
- IOS学习笔记08-创建图片浏览程序
- hdu5399 Too simple
- JS的继承
- 2015/8/18/Activity框图/Linear与RelativeLayout布局
- MD5加密(Java)
- 如何动笔写你的第一篇论文
- uva216 Getting in Line
- 黑马程序员——IO(缓冲区、字节流、转换流、流操作规律)
- sqlite 数据库操作 创建数据库
- WPF中StaticResource 标记扩展和DynamicResource 标记扩展
- hdu5400(计数)