小念子

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

string s,表示s是一个string类的对象,有自己的成员变量和成员函数

为了和上一篇的结尾呼应,先说明string类对象的sizeof的结果:

string s = “ahdskahlal”;

sizeof(s) = 32(x86)或者64(x64);

 

下面介绍和string类相关的函数

 http://www.cppblog.com/lmlf001/archive/2006/04/19/5883.html

这篇文章介绍的很全,下面贴一些代码加深印象(其实这是百度的总结在一起了)

#include <iostream>
#include <string>
#include <sstream> 
using namespace std;

int main()
{
    //1.string类重载运算符operator>>用于输入,同样重载运算符operator<<用于输出操作
    string str1;
    getline(cin, str1);//字符串的行输入
    cout << str1 << endl; 
    cout<<endl;

    //2.string类的构造函数 
    char *s = "bbbbb";
    string str2(s);//用c字符串s初始化 
    cout << str2 << endl;

    char ch = 'c';
    string str3(5, ch);//用n个字符ch初始化 
    cout << str3 << endl; 
    cout<<endl;

    //3.string类的字符操作
    string str4 = "abcde";
    ch = str4.at(4);//at()返回当前字符串中第n个字符的位置,并且提供范围检查,当越界时会抛出异常!  
    cout << ch << endl; 
    cout<<endl;

    //4.string的特性描述
    string str5 = "abcdefgh";
    int size;
    size = str5.capacity();//返回当前容量,一般在x86上都是输出15
    cout << size << endl; 
    size = str5.max_size();//返回string对象中可存放的最大字符串的长度 ,一般在x86上都是输出-2
    cout << size << endl; 
    size = str5.size();//返回当前字符串的大小 
    cout << size << endl; 
    size = str5.length();//返回当前字符串的长度 
    cout << size << endl; 
    int len = 10; 
    str5.resize(len, ch);//把字符串当前大小置为len,并用字符ch填充不足的部分 
    cout << str5 << endl; 
    cout<<endl;

    //5.string的赋值
    string str6;
    str6.assign(str5);//把字符串str5赋给当前字符串 
    cout << str6 << endl; 
    str6.assign(s);//用c类型字符串s赋值 
    cout << str6 << endl; 
    str6.assign(s, 2);//用c类型字符串s开始的n个字符赋值 
    cout << str6 << endl; 
    str6.assign(len, ch);//用len个字符ch赋值给当前字符串 
    cout << str6 << endl; 
    str6.assign(str5, 0, 3);//把字符串str7中从0开始的3个字符赋给当前字符串 
    cout << str6 << endl; 
    cout<<endl;

    //6.string的连接,用append
    string com;
    com += str6;//把字符串str9连接到当前字符串的结尾 
    cout << com << endl;
    com.append(s);//把c类型字符串s连接到当前字符串的结尾 
    cout << com << endl; 
    com.append(s, 2);//把c类型字符串s的前2个字符连接到当前字符串的结尾 
    cout << com << endl; 
    com.push_back('k');//把一个字符连接到当前字符串的结尾 
    cout << com << endl; 
    cout<<endl;

    //7.string的比较 ,用compare
    //以下的3个函数同样适用于c类型的字符串,在compare函数中>时返回1,<时返回-1,=时返回0
    int flag;
    flag = com.compare(str6);//比较两个字符串的大小,通过ASCII的相减得出! 
    cout << flag << endl; 
    flag = com.compare(6, 12, str6);//比较com字符串从6开始的12个字符组成的字符串与str6的大小 
    cout << flag << endl;
    flag = com.compare(6, 12, str6, 3, 5);//比较com字符串从6开始的12个字符组成的字符串与str6字符串从3开始的5个字符组成的字符串的大小 
    cout << flag << endl; 
    cout<<endl;

    //8.string的子串
    string str7;
    str7= com.substr(10, 15);//返回从下标10开始的15个字符组成的字符串 
    cout << str7 << endl; 
    cout<<endl;

    //9.string的交换
    str7.swap(com);//交换str7与com的值 
    cout << str7 << endl; 
    cout<<endl;

    //10.string的查找,查找成功时返回所在位置,失败时返回string::npos的值,即是-1 
    string str8 = "abcdefghijklmnopqrstuvwxyz";
    int pos;
    pos = str8.find('i', 0);//从位置0开始查找字符i在当前字符串的位置 
    cout << pos << endl;
    pos = str8.find("ghijk", 0);//从位置0开始查找字符串“ghijk”在当前字符串的位置 
    cout << pos << endl; 
    pos = str8.find("opqrstuvw", 0, 4);//从位置0开始查找字符串“opqrstuvw”前4个字符组成的字符串在当前字符串中的位置 
    cout << pos << endl; 
    pos = str8.rfind('s', string::npos);//从字符串str8反向开始查找字符s在字符串中的位置 
    cout << pos << endl; 
    pos = str8.rfind("klmn", string::npos);//从字符串str8反向开始查找字符串“klmn”在字符串中的位置 
    cout << pos << endl;
    pos = str8.rfind("opqrstuvw", string::npos, 3);//从string::pos开始从后向前查找字符串s中前n个字符组成的字符串在当前串中的位置 
    cout << pos << endl; 
    cout<<endl;

    string str9 = "aaabbbcccddeeffgghhiijjkkllmmandjfaklsdfpopdtwptioczx";
    pos = str9.find_first_of('d', 0);//从位置0开始查找字符d在当前字符串第一次出现的位置 
    cout << pos << endl; 
    pos = str9.find_first_of("eefff", 0);//从位置0开始查找字符串“eeefff“在当前字符串中第一次出现的位置 
    cout << pos << endl; 
    pos = str9.find_first_of("efff", 0, 3);//从位置0开始查找当前串中第一个在字符串”efff“的前3个字符组成的数组里的字符的位置 
    cout << pos << endl;
    pos = str9.find_first_not_of('b', 0);//从当前串中查找第一个不在串s中的字符出现的位置 
    cout << pos << endl; 
    pos = str9.find_first_not_of("abcdefghij", 0);//从当前串中查找第一个不在串s中的字符出现的位置 
    cout << pos << endl; 
    pos = str9.find_first_not_of("abcdefghij", 0, 3);//从当前串中查找第一个不在由字符串”abcdefghij”的前3个字符所组成的字符串中的字符出现的位置 
    cout << pos << endl; 
    //下面的last的格式和first的一致,只是它从后面检索! 
    pos = str9.find_last_of('b', string::npos);
    cout << pos << endl;
    pos = str9.find_last_of("abcdef", string::npos);
    cout << pos << endl;
    pos = str9.find_last_of("abcdef", string::npos, 2);
    cout << pos << endl; 
    pos = str9.find_last_not_of('a', string::npos);
    cout << pos << endl; 
    pos = str9.find_last_not_of("abcdef", string::npos);
    cout << pos << endl;
    pos = str9.find_last_not_of("abcdef", string::npos, 3);
    cout << pos << endl;
    cout<<endl;

    //11.string的替换,用replace
    string str10 = "abcdefghijklmn";
    str10.replace(0, 3, "qqqq");//删除从0开始的3个字符,然后在0处插入字符串“qqqq” 
    cout << str10 << endl; 
    str10.replace(0, 3, "vvvv", 2);//删除从0开始的3个字符,然后在0处插入字符串“vvvv”的前2个字符 
    cout << str10 << endl; 
    str10.replace(0, 3, "opqrstuvw", 2, 4);//删除从0开始的3个字符,然后在0处插入字符串“opqrstuvw”从位置2开始的4个字符 
    cout << str10 << endl; 
    str10.replace(0, 3, 8, 'c');//删除从0开始的3个字符,然后在0处插入8个字符 c 
    cout << str10 << endl; 
    cout<<endl;


    //12.string的插入
    string str11 = "abcdefg";
    str11.insert(0, "mnop");//在字符串的0位置开始处,插入字符串“mnop” 
    cout << str11 << endl; 
    str11.insert(0, 2, 'm');//在字符串的0位置开始处,插入2个字符m 
    cout << str11 << endl; 
    str11.insert(0, "uvwxy", 3);//在字符串的0位置开始处,插入字符串“uvwxy”中的前3个字符 
    cout << str11 << endl;
    str11.insert(0, "uvwxy", 1, 2);//在字符串的0位置开始处,插入从字符串“uvwxy”的1位置开始的2个字符 
    cout << str11 << endl; 
    cout<<endl;

    //13.string的删除
    string str12 = "gfedcba";
    string::iterator it;
    it = str12.begin();
    it++;
    str12.erase(it);//删除it指向的字符,返回删除后迭代器的位置 
    cout << str12 << endl;
    str12.erase(it, it+3);//删除it和it+3之间的所有字符,返回删除后迭代器的位置 
    cout << str12 << endl; 
    str12.erase(2);//删除从字符串位置3以后的所有字符,返回位置3前面的字符 
    cout << str12 << endl; 
    cout<<endl;

    //14.字符串的流处理
    string str13("hello,this is a test");
    istringstream is(str13);
    string s1,s2,s3,s4;
    is>>s1>>s2>>s3>>s4;//s1="hello,this",s2="is",s3="a",s4="test"
    ostringstream os;
    os<<s1<<s2<<s3<<s4;
    cout<<os.str() << endl;

    system("pause");
}

 两外还有三个和string相关的成员函数,从一个string得到c类型的字符数组:c_str()、data()、copy(p,n)。

c_str()\data():生成一个const char*指针,指向以\不以空字符终止的数组。

    这个数组的数据是临时的,当有一个改变这些数据的成员函数被调用后,其中的数据就会失效,因此要么现用现转换,要么复制到用户自己可以管理的内存中,运用strcpy()函数来复制。比如下面的例子(换成c = s.data()也一样):

#include<iostream>
#include<string>
using namespace std;
int main()
{
    const char* c;
    string s="1234";
    c = s.c_str(); 
    cout<<c<<endl; //输出:1234

    //将c复制到用户管理的内存myself中
    char * myself = new char[10];
    strcpy(myself,c);

    s="abcd";
    cout<<c<<endl; //输出:abcd 
    cout<<myself<<endl; //输出:1234
}

copy(p,n,size_type _Off = 0):从string类型对象中至多复制n个字符到字符指针p指向的空间中。默认从首字符开始,但是也可以指定,开始的位置(记住从0开始)。返回真正从对象中复制的字符。------用户要确保p指向的空间足够保存n个字符

#include<iostream>
#include<string>
using namespace std;
int main()
{
    string s="1234abdd";
    char dest[20] = {'\0'};
    int n = s.copy(dest,10);
    cout<<n<<endl;     //输出:8
    cout<<dest<<endl;    //输出:1234abdd

    string ss = "xyz";
    n = ss.copy(dest,2,1);
    cout<<n<<endl;  //输出:2
    cout<<dest<<endl;   //输出:yz34abdd
}

 

posted on 2014-03-20 21:45  小念子  阅读(3952)  评论(0编辑  收藏  举报