• string类的常用成员函数


    1、与字符串对象输入输出相关的函数

    (1)使用string输入输出
    • 除了标准流和文件流输入输出外,还可以从string进行输入输出;
    • 类似 istream和osteram进行标准流输入输出,我们用istringstream 和ostringstream进行字符串上的输入输出,也称为内存输入输出。
    (2)需要包含的头文件

    #include <string>
    #include <iostream>
    #include <sstream>

    string input("Input test 123 4.7 A");
    istringstream inputString(input);
    string string1, string2;
    int i;
    double d;
    char c;
    inputString >> string1 >> string2 >> i >> d >> c;
    cout << string1 << endl << string2 << endl;
    cout << i << endl << d << endl << c <<endl;
    long L;
    if(inputString >> L) cout << "long
    ";
    else cout << "empty
    ";
    /*输出:
    Input
    test
    123
    4.7
    A
    empty
    */
    
    ostringstream outputString;
    int a = 10;
    outputString << "This " << a << "ok" << endl;
    cout << outputString.str();
    /*输出:
    This 10ok
    */
    
    (3)从流中读取一个string对象
    cin.operator>>(string& str)//cin 中重载的>>的符号函数,有接收字符串的版本
    
    /*
    函数功能:
    从流中读取一个字符串到字符对象中。
    */
    
    //使用举例
    	string stringObject;
    	cin >> stringObject;
    
    (4)从流中读取一行
    getline(cin,string);
    
    /*
    函数功能:
    从流中读取一行到字符对象中。
    */
    
    //使用举例
    	string s;
    	getline(cin ,s);
    

    2、string类对象的长度获取

    (1)str1.length()
    //成员函数
    string str1;
    str1.length()
    
    /*
    函数功能:
    获取字符对象的长度。
    */
    
    //使用举例
    	string s("hello");
    	cout << s.length() << endl;
    
    (2) str1.size()
    //成员函数
    string str1;
    str1.size()
    
    /*
    函数功能:
    获取字符对象的长度。
    */
    
    //使用举例
    	string s("hello");
    	cout << s.size() << endl;
    

    3、赋值拼接

    (1)用 = 赋值
    //string 中重载的=的符号函数。
    basic_string& operator=( const basic_string& str );
    /*
    函数功能:
    将一个string对象的值赋值给等号左边
    */
    
    //使用举例
    	string s1("cat"), s2;
    	s2 = s1;
    
    (2)用 assign 成员函数复制全部或者部分
    basic_string& assign( const basic_string& str );
    basic_string& assign( const basic_string& str,size_type pos, size_type count ); 
    /*
    函数功能:
    将一个string对象的全部或者部分复制到另一个字符串对象中。
    */
    
    //使用举例
    	//复制全部
    	string s1("cat"), s3;
    	s3.assign(s1);
    	//复制部分
    	string s1("catpig"), s3;
    	s3.assign(s1, 1, 3);//从s1 中下标为1的字符开始复制3个字符给s3
    
    (3)单个字符赋值
    //string 类中重载了[] 符号
    reference operator[]( size_type pos );
    const_reference operator[]( size_type pos ) const;
    /*
    函数功能:
    单个字符赋值
    */
    //使用举例:
    	s2[5] = s1[3] = 'a';
    
    (4)at成员函数:逐个访问string对象中的字符
    reference       at( size_type pos );  
    const_reference at( size_type pos ) const; 
    /*
    函数功能:
    返回到位于指定位置 pos 的字符的引用。进行边界检查,非法访问时抛出 std::out_of_range 类型的异常。
    */
    
    //使用举例:
    	string s1("Hello");
    	for(int i=0;i<s1.length();i++)
    		cout << s1.at(i) << endl;
    

    注意:成员函数at会做范围检查,如果超出范围,会抛出out_of_range异常,而下标运算符[]不做范围检查。

    (5)用 + 运算符连接字符串
    string.operator+()//string 类中重载了+ 符号
    /*
    函数功能:
    拼接字符对象中的字符串。
    */
    //使用举例:
    	string s1("good "), s2("morning! ");
    	s1 += s2;
    	cout << s1;
    
    (6)用成员函数 append 连接字符串
    string.append()
    /*
    函数功能:
    拼接字符对象中的字符串。
    */
    //使用举例:
    	string s1("good "), s2("morning! ");
    	s1.append(s2);
    	cout << s1;
    	s2.append(s1, 3, s1.size());//s1.size(),s1字符数
    	cout << s2;
    

    4、比较string的大小

    (1)用关系运算符比较
    //string 类中重载了== , >, >=, <, <=, != 符号
    //以==为例                     
    
    template< class CharT, class Traits, class Alloc >
    bool operator==( const CharT* lhs, const basic_string<CharT,Traits,Alloc>& rhs );
    
    template< class CharT, class Traits, class Alloc >
    bool operator==( const basic_string<CharT,Traits,Alloc>& lhs, const CharT* rhs ); 
    
    /*
    函数功能:
    比较 string 与另一 string 或 CharT 的空终止数组的内容。返回值都是bool类型,成立返回true, 否则返回false。
    */
    //使用举例:
    	string s1("hello"),s2("hello"),s3("hell");
    	bool b = (s1 == s2);
    	cout << b << endl;
    	b = (s1 == s3);
    	cout << b << endl;
    	b = (s1 > s3);
    	cout << b << endl;
    /*输出:
    1
    0
    1
    */
    
    (2)用成员函数compare比较string的大小
    int compare( const basic_string& str ) const;			//c++11 前
    int compare( const basic_string& str ) const noexcept;  //c++11 起
    /*
    函数功能:
    比较字符串的大小。
    */
    //使用举例:
    	string s1("hello"),s2("hello"),s3("hell");
    	int f1 = s1.compare(s2);
    	int f2 = s1.compare(s3);
    	int f3 = s3.compare(s1);
    	int f4 = s1.compare(1,2,s3,0,3); //s1 1-2; s3 0-3
    	int f5 = s1.compare(0,s1.size(),s3);//s1 0-end
    	cout << f1 << endl << f2 << endl << f3 << endl;
    	cout << f4 << endl << f5 << endl;
    /*
     0 	 // hello == hello
    -1	 // hell < hello
    -1	 // el < hell
     1	 // hello > hell
     1	 // hello > hell
    */
    

    5、成员函数 substr

    basic_string substr( size_type pos = 0,size_type count = npos ) const;
    /*函数功能:
    返回子串 [pos, pos+count) 。若请求的子串越过 string 的结尾,或若 count == npos ,则返回的子串为 [pos, size()) 。
    返回值:
    含子串 [pos, pos+count) 的 string。
    */
    

    6、交换字符串对象中的字符串

    void swap( basic_string& other );							//(C++17 前) 
    void swap( basic_string& other ) noexcept(/* see below */);	//(C++17 起) 
    /*
    函数功能:
    交换 string 与 other 的内容。可能非法化所有迭代器和引用。
    */
    //使用举例:
    string s1("hello world"), s2("really");
    s1.swap(s2);
    cout << s1 << endl;
    cout << s2 << endl;
    /*输出:
    really
    hello world
    */
    

    7、寻找string中的字符

    (1)成员函数 find()
    str1.find()
    /*
    函数功能:
    查找字符串
    */
    //使用举例:
    	string s1("hello world");
    	s1.find("lo");
    //在s1中从前向后查找 “lo” 第一次出现的地方,如果找到,返回 “lo”开始的位置,即 l 所在的位置下标。如果找不到,返回string::npos (string中定义的静态常量)
    
    (2)成员函数 rfind()
    string.rfind()
    /*
    函数功能:
    查找字符串
    */
    //使用举例:
    	string s1("hello world");
    	s1.rfind("lo");
    //在s1中从后向前查找 “lo” 第一次出现的地方,如果找到,返回 “lo”开始的位置,即 l 所在的位置下标。如果找不到,返回string::npos 。
    
    (3)成员函数 find_first_of()
    string.find_first_of()
    /*
    函数功能:
    查找字符串
    */
    //使用举例:
    	string s1("hello world");
    	s1.find_first_of(“abcd");
    //在s1中从前向后查找 “abcd” 中任何一个字符第一次出现的地方,如果找到,返回找到字母的位置,如果找不到,返回string::npos。
    
    (4)成员函数 find_last_of()
    find_last_of()
    /*
    函数功能:
    查找字符串
    */
    //使用举例:
    	string s1("hello world")
    	s1.find_last_of(“abcd");
    //在s1中查找 “abcd” 中任何一个字符最后一次出现的地方,如果找到,返回找到字母的位置,如果找不到,返回string::npos。
    
    (5)成员函数 find_first_not_of()
    string.find_first_not_of()
    /*
    函数功能:
    查找字符串
    */
    //使用举例:
    	string s1("hello world");
    	s1.find_first_not_of(“abcd");
    //在s1中从前向后查找不在 “abcd” 中的字母第一次出现的地方,如果找到,返回找到字母的位置,如果找不到,返回string::npos。
    
    (6)成员函数 find_last_not_of()
    find_last_not_of()
    /*
    函数功能:
    查找字符串
    */
    //使用举例:
    	string s1("hello world");
    	s1.find_last_not_of(“abcd");
    	//在s1中从后向前查找不在 “abcd” 中的字母第一次出现的地方,如果找到,返回找到字母的位置,如果找不到,返回string::npos。
    

    8、删除string中的字符

    erase()
    /*
    函数功能:
    删除字符串对象中的字符。
    */
    //使用举例:
    	string s1("hello worlld");
    	s1.erase(5);// 去掉下标 5 及之后的字符
    	cout << s1;
    	cout << s1.length();
    	cout << s1.size();
    /*
    输出:
    hello55
    */
    

    9、 替换string中的字符

    replace()
    /*
    函数功能:
    替换字符串
    */
    //使用举例:
    	string s1("hello world");
    	s1.replace(2,3, “haha");
    	cout << s1;//将s1中下标2 开始的3个字符换成“haha”
    /*
    输出:
    hehaha world
    */
    
    //应用举例2:
    string s1("hello world");
    s1.replace(2,3, "haha", 1,2);
    cout << s1;
    

    10、在string中插入字符

    insert()
    /*
    函数功能:
    查找字符串
    */
    //应用举例:
    	string s1("hello world");
    	string s2(“show insert");
    	s1.insert(5,s2); // 将s2插入s1下标5的位置
    	cout << s1 << endl;
    	s1.insert(2,s2,5,3);//将s2中下标5开始的3个字符插入s1下标2的位置
    	cout << s1 << endl;
    /*
    输出:
    helloshow insert world
    heinslloshow insert world
    */
    

    11、 转换成C语言式char *字符串

    (1)string.c_str()
    string s1("hello world");
    printf("%s
    ", s1.c_str());
    // s1.c_str() 返回传统的const char * 类型字符串,且该字符串以‘’结尾。
    /*输出
    hello world
    */
    
    (2)string.data()
    	string s1("hello world");
    	const char * p1=s1.data();
    	for(int i=0;i<s1.length();i++)
    	printf("%c",*(p1+i));////s1.data() 返回一个char * 类型的字符串,对s1 的修改可能会使p1出错。
    /*输出:
    hello world
    */
    

    12、字符串拷贝

    string.copy()
    /*
    函数功能:
    将一个字符串拷贝给另外一个字符串对象。
    */
    	string s1("hello world");
    	int len = s1.length();
    	char * p2 = new char[len+1];
    	s1.copy(p2,5,0);
    	p2[5]=0;
    	cout << p2 << endl;//// s1.copy(p2,5,0) 从s1的下标0的字符开始制作一个最长5个字符长度的字符串副本并将其赋值给p2。返回值表明实际复制字符串的长度。
    
    /*输出:
    hello
    */
    

    注意:

    • 这里只是简单提了一下标准库中的string类的常用成员函数。更详细内容可以看cpp conference。
    • 可以发现,c++标准库中许多类或函数的实现使用到了STL中定义的类模板或者函数模板。
  • 相关阅读:
    Eclipse快捷键大全
    如何查看JDK_API 2019.2.23
    JXL、POI操作Excel
    Eclipse 将builder文件夹下的classes文件改路径到WEB-INF下,以及常用快捷键
    系统分盘
    U盘被识别为其他设备(显示U盘图标但是不显示盘符)的解决办法
    Oracle+jsp+Servlet的员工表的简单增删改查
    2019年3月8日09:06:02 mybatis 一对多
    linux 协议栈分享
    fib
  • 原文地址:https://www.cnblogs.com/lasnitch/p/12764223.html
Copyright © 2020-2023  润新知