zoukankan      html  css  js  c++  java
  • C++ string 字符串 结尾 标志

    看个示例

    #include<iostream>
    #include<string>
    using namespace std;
     
    int main()
    {
    	string str = "hello";
    	str[3] = '';
    	cout << str << endl;
    	return 0;	
    } 
    
    输出:hel o
    

    应该明白了点什么!在C++中不能作为识别string类字符串的结尾
    其实C++string类也不需要识别结尾,因为你需要用到的C++的函数库基本都帮你解决了

    再看个例子

    #include<iostream>
    #include<string>
    using namespace std;
     
    int main()
    {
    	string str = "hello";
    	int i = 0,len = 0;
    	while(str[i++])
    	{
    		++len;
    	}
    	cout << "len = " << len << endl;
    	cout << "str.length() = " << str.length() << endl;
    	return 0;	
    } 
    
    输出:
    len = 5
    str.length() = 5
    

    两个值len和str.length()相等,说明len的计算值是对的,也就是说string类结尾处还是有的。说明一下我用的dev编译运行的。至于 这是编译器产商加进去的(根据具体实现而言,有的编译器就没加),并非C++本身所要求的,C++并没有要求string类要有结尾

  • 相关阅读:
    GUI编程
    Markdown学习
    [python3]正则表达式
    python3_json&pickle
    python3_module_sys
    python3_module_os
    Python3_module_random
    Pyhton3_module_time()
    Python3 正则表达式 Regular Expression
    Python循环对象
  • 原文地址:https://www.cnblogs.com/lixuejian/p/13153294.html
Copyright © 2011-2022 走看看