33-C++中的字符串类

来源:互联网 发布:凌哥刷枪软件 编辑:程序博客网 时间:2024/05/21 01:47

1、

这里写图片描述

2、

这里写图片描述

#include <iostream>#include <string>using namespace std;void string_sort(string a[], int len){    for(int i=0; i<len; i++)    {        for(int j=i; j<len; j++)        {            if( a[i] > a[j] )            {                swap(a[i], a[j]);            }        }    }}string string_add(string a[], int len){    string ret = "";    for(int i=0; i<len; i++)    {        ret += a[i] + "; ";    }    return ret;}int main(){    string sa[7] =     {        "Hello World",        "D.T.Software",        "C#",        "Java",        "C++",        "Python",        "TypeScript"    };    string_sort(sa, 7);    for(int i=0; i<7; i++)    {        cout << sa[i] << endl;    }    cout << endl;    cout << string_add(sa, 7) << endl;    return 0;}

3、

这里写图片描述

4、

这里写图片描述

5、

这里写图片描述

string to int to double and int double to string

stringsteam

#include <iostream>#include <sstream>#include <string>using namespace std;#define TO_NUMBER(s, n) (istringstream(s) >> n)#define TO_STRING(n) (((ostringstream&)(ostringstream() << n)).str())int main(){    double n = 0;    if( TO_NUMBER("234.567", n) )    {        cout << n << endl;        }    string s = TO_STRING(12345);    cout << s << endl;         return 0;}

6、

这里写图片描述

#include <iostream>#include <string>using namespace std;string operator >> (const string& s, unsigned int n){    string ret = "";    unsigned int pos = 0;    n = n % s.length();    pos = s.length() - n;    ret = s.substr(pos);    ret += s.substr(0, pos);    return ret;}int main(){    string s = "abcdefg";    string r = (s >> 3);    cout << r << endl;    return 0;}

7、

这里写图片描述

原创粉丝点击