c++ string vector类

//string对象的初始化 
#include <iostream> 
#include <string> //typedef std::basic_string<char> string; 
using namespace std;

typedef string String;

int main() 
{
    // with no arguments     
    string s1;          //默认构造函数:没有参数或参数有默认值     
    String s2("hello"); //普通构造函数 String就是string     
    s1 = "Anatoliy";    //赋值运算符      
    String s3(s1);   //拷贝构造函数 string s3 =s1;     
    cout << "s1 is: " << s1 << endl;
    cout << "s2 is: " << s2 << endl;
    cout << "s3 is: " << s2 << endl;

    // first argument C string     
    // second number of characters     
    string s4("this is a C_sting", 10);
    cout << "s4 is: " << s4 << endl;

    // 1 - C++ string     
    // 2 - start position     
    // 3 - number of characters     
    string s5(s4, 6, 4); // copy word from s3     cout << "s5 is: " << s5 << endl;

    // 1 - number characters     
    // 2 - character itself     
    string s6(15, '*');
    cout << "s6 is: " << s6 << endl;

    // 1 - start iterator(迭代器 )     
    // 2 - end iterator(迭代器 )     
    string s7(s4.begin(), s4.end() - 5);
    cout << "s7 is: " << s7 << endl;

    // 通过=初始化string对象     
    string s8 = "Anatoliy";
    cout << "s8 is: " << s8 << endl;

    string s9 = s1 + "hello"+ s2;    //s1 + "hello"+ s2的结果是string类型的对象(变量)     
    cout << "s9 is: " << s9 << endl;
    return 0;
}

s1 is: Anatoliy
s2 is: hello
s3 is: Anatoliy
s4 is: this is aC
s5 is: s aC
s6 is: ***************
s7 is: this
s8 is: Anatoliy
s9 is: Anatoliyhellohello

 

原文链接: https://www.cnblogs.com/CodeWorkerLiMing/p/10997897.html

欢迎关注

微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍

    c++ string vector类

原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/296476

非原创文章文中已经注明原地址,如有侵权,联系删除

关注公众号【高性能架构探索】,第一时间获取最新文章

转载文章受原作者版权保护。转载请注明原作者出处!

(0)
上一篇 2023年2月15日 下午5:54
下一篇 2023年2月15日 下午5:55

相关推荐