转换到C样式数组
来源:互联网 发布:ip mac 扫描工具 编辑:程序博客网 时间:2024/06/06 02:58
char& string::operator[] (size_type nIndex)const char& string::operator[] (size_type nIndex) constBoth of these functions return the character with index nIndexPassing an invalid index results in undefined behaviorUsing length() as the index is valid for const strings only, and returns the value generated by the string’s default constructor. It is not recommended that you do this.Because char& is the return type, you can use this to edit characters in the arraySample code:string sSource("abcdefg");cout << sSource[5] << endl;sSource[5] = 'X';cout << sSource << endl;Output:fabcdeXg
还有一个非算子的版本。这个版本是慢因为它使用异常来检查如果nIndex是有效的。如果你不确定是否是有效的索引,你应该使用此版本访问数组:
Sample code:string sSource("abcdefg");cout << sSource.at(5) << endl;sSource.at(5) = 'X';cout << sSource << endl;Output:fabcdeXg
转换到C样式数组
多功能(包括所有的C函数)预计,字符串被格式化为C风格字符串而不是std::string。为此,std::string提供了3种不同的方式来转换std::字符串C风格字符串。
Sample code:string sSource("sphinx of black quartz, judge my vow");char szBuf[20];int nLength = sSource.copy(szBuf, 5, 10);szBuf[nLength]='\0'; // Make sure we terminate the string in the buffercout << szBuf << endl;Output:black
0 0
- 转换到C样式数组
- 转换到C样式数组
- 转换到C样式数组
- CString 到char*:转换要求 reinterpret_cast、C 样式转换或函数样式转换
- CString 转换到数组
- 换要求 reinterpret_cast、C 样式转换或函数样式转换
- C/C++左值性精髓(三)左值转换----从数组到指针的转换
- object-c 转换到C
- 字符串转换到double数组
- short数组 到 byte 数组的转换
- 转换要求reinterpret_cast、C样式…
- ArrayList的示例系列 - 如何转换数组到ArrayList在C#
- 与指向的类型无关;转换要求 reinterpret_cast、C 样式转换或函数样式转换
- 与指向的类型无关;转换要求 reinterpret_cast、C 样式转换或函数样式转换
- 与指向的类型无关;转换要求 reinterpret_cast、C 样式转换或函数样式转换
- 与指向的类型无关;转换要求 reinterpret_cast、C 样式转换或函数样式转换
- 与指向的类型无关;转换要求 reinterpret_cast、C 样式转换或函数样式转换
- 与指向的类型无关;转换要求 reinterpret_cast、C 样式转换或函数样式转换
- 理解MVC设计模式
- 基类的源代码
- identify()被调用时
- 它在我们的数据
- 大话重构连载12:你不能没有保险索
- 转换到C样式数组
- [xcode]常见错误
- 函数模板实例
- 代码可以编译和运行
- uPortal4.1.0安装部署
- 哎呀头疼的驱动
- zoj 3811||牡丹江网赛 c题 并查集
- Android应用开发-小巫CSDN博客客户端开发开篇
- 开源HTML5 APP开发神器CanTK发布