C++ string

C++ string,第1张

  • 初始化string 对象的方式
    string s1;默认构造函数 s1 为空串
    string s2(s1);将 s2 初始化为 s1 的一个副本
    string s3(“value”);将 s3 初始化为一个字符串字面值副本
    string s4(n, ‘c’);将 s4 初始化为字符 ‘c’ 的 n 个副本
  • string 对象的读写

    • 读取并忽略开头所有的空白字符(如空格,换行符,制表符)。



    • 读取字符直至再次遇到空白字符,读取终止。



    1、读取一行

    int main()
    {
    string s; // empty string
    cin >> s; // read whitespace-separated string into s //输入hello world
    cout << s << endl; // write s to the output	//输出hello
    return 0;
    }
    

    2、可以把多个读 *** 作或多个写 *** 作放在一起

    string s1, s2;
    cin >> s1 >> s2; // read first input into s1, second into s2     //输入 hello world
    cout << s1 << s2 << endl; // write both strings		//输出 helloworld
    

    3、读入未知数目的string 对象
    string 的输入 *** 作符也会返回所读的数据流,因此,可以把输入 *** 作作为判断条件

    int main()
    {
    	string word;
    	// read until end-of-file, writing each word to a new line
    	while (cin >> word)
    	cout << word << endl;
    	return 0;
    }
    

    4、使用getline 读取整行文本
    getline 并不忽略行开头的换行符


    只要 getline 遇到换行符,即便它是输入的第一个字符,getline 也将停止读入并返回

    int main()
    {
    	string line;
    	// read line at time until end-of-file
    	while (getline(cin, line))
    	cout << line << endl;
    	return 0;
    }
    
  • string 对象的 *** 作
    s.empty()如果 s 为空串,则返回 true,否则返回 false。


    s.size()返回 s 中字符的个数
    s[n]返回 s 中位置为 n 的字符,位置从 0 开始计数
    s1 + s2把 s1 和s2 连接成一个新字符串,返回新生成的字符串
    s1 = s2把 s1 内容替换为 s2 的副本
    v1 == v2比较 v1 与 v2 的内容,相等则返回 true,否则返回 false
    !=, <, <=, >, and >=保持这些 *** 作符惯有的含义
  • string::size_type 类型

    string size() *** 作返回的是 string::size_type 类型的值,是 unsigned int型

  • string 关系 *** 作符

    string 对象比较 *** 作是区分大小写的,任何一个大写之母都小于任意的小写字母。



    比较策略:
    • 如果两个 string 对象长度不同,且短的 string 对象与长的 string 对象的前面部分相匹配,则短的 string 对象小于长的 string 对象。



    • 如果 string 对象的字符不同,则比较第一个不匹配的字符

  • string 对象的赋值
    // st1 is an empty string, st2 is a copy of the literal
    string st1, st2 = "The expense of spirit";
    st1 = st2; // replace st1 by a copy of st2
    
  • 两个string 对象相加
    string s1("hello, ");
    string s2("world\n");
    string s3 = s1 + s2; // s3 is hello, world\n
    
  • 和字符串字面值的连接

    + *** 作符的左右 *** 作数必须至少有一个是 string 类型的

    string s1 = "hello"; // no punctuation
    string s2 = "world";
    string s3 = s1 + ", "; // ok: adding a string and a literal
    string s4 = "hello" + ", "; // error: no string operand
    string s5 = s1 + ", " + "world"; // ok: each + has string operand
    string s6 = "hello" + ", " + s2; // error: can't add string literals
    
  • 从string 对象获取字符

    string 类型通过下标 *** 作符([ ])来访问 string 对象中的单个字符。


    下标 *** 作符需要取一个 size_type 类型的值,来标明要访问字符的位置。



    string 对象的下标从 0 开始。


    如果 s 是一个 string 对象且 s 不空,则 s[0] 就是字符串的第一个字符, s[1] 就表示第二个字符(如果有的话),而 s[s.size() - 1] 则表示 s 的最后一个字符。



    string 对象的索引变量最好也用 string::size_type 类型


    string str("some string");
    for (string::size_type ix = 0; ix != str.size(); ++ix)
    cout << str[ix] << endl;
    
  • string find函数
    c, pos在 s 中,从下标 pos 标记的位置开始,查找字符 c。


    pos 的默认值为 0

    s2, pos在 s 中,从下标 pos 标记的位置开始,查找 string 对象 s2。


    pos 的默认值为 0

    cp, pos在 s 中,从下标 pos 标记的位置形参,查找指针 cp 所指向的 C 风格的以空字符结束的字符串。


    pos 的默认值为 0

    cp, pos, n在 s 中,从下标 pos 标记的位置开始,查找指针 cp 所指向数组的前 n 个字符。


    pos 和 n 都没有默认值

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/langs/562431.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-04-01
下一篇 2022-04-01

发表评论

登录后才能评论

评论列表(0条)

保存