STL之string类成员函数

2022-06-16 14:32:42 浏览数 (1)

string成员函数

总体函数

代码语言:javascript复制
#include <string>
#include <iostream>

using namespace std;

int main()
{
   //构造函数
   string str1 = "hello"; 
   string* str2 = new string("hello");
   string str3 = "world";

   //获取字符长度
   int n = str1.length();
    
    //字符拼接
    string str4 = str1   str3;

    //获取字符串第一个字符
    string::const_iterator it = str1.begin();
    cout << *it << endl;

    it = str1.end();
    it--;
    cout << *it << endl;

    //字符串倒置
    reverse(str1.begin(), str1.end());

    //字符串转字符数组
    string c = "abc123";
    char *d = new char[20];
    strcpy(d, c.c_str());//因为这里没有直接赋值,所以指针类型可以不用const char *
    
    //访问
    cout<<str1[1]<<endl;
    cout<<str1.at(1)<<endl;

    //查找
 string st1("babbabab");

cout << st1.find('a') << endl;//1,默认从位置0(即第1个字符)开始查找

cout << st1.find('a', 2) << endl;//4 在st1中,从位置2(b,包括位置2)开始,查找a,返回首次匹配的位置

cout << (st1.find('c', 0) == -1) << endl;//1

string st2("aabcbcabcbabcc");

str1 = "abc";

cout << st2.find(str1, 2) << endl;//6,从st2的位置2(b)开始匹配,返回第一次成功匹配时匹配的串(abc)的首字符在st2中的位置,失败返回-1

cout << st2.find("abcdefg", 2, 3) << endl;//6 取abcdefg得前3个字符(abc)参与匹配,相当于st2.find("abc", 2)

//rfind-从指定位置起向前查找,直到串首

cout << st1.rfind('a', 7) << endl;//6

//find_first_of-在源串中从位置pos起往后查找,只要在源串中遇到一个字符,该字符与目标串中任意一个字符相同,就停止查找,返回该字符在源串中的位置;若匹配失败,返回-1

string str6("bcgjhikl");

string str7("kghlj");

cout << str6.find_first_of(str7, 0) << endl;//2,从str1的第0个字符b开始找,g与str2中的g匹配,停止查找,返回g在str1中的位置2

//find_last_of-与find_first_of函数相似,只不过查找顺序是从指定位置向前

string str("abcdecg");

cout << str.find_last_of("hjlywkcipn", 6) << endl;//5,从str的位置6(g)开始向前找,g不匹配,再找c,c匹配,停止查找,返回c在str中的位置5

//find_first_not_of-在源串中从位置pos开始往后查找,只要在源串遇到一个字符,与目标串中的任意字符都不相同,就停止查找,返回该字符在源串中的位置;若遍历完整个源串,都找不到满足条件的字符,则返回-1

cout << str.find_first_not_of("kiajbvehfgmlc", 0) << endl;//3 从源串str的位置0(a)开始查找,目标串中有a,匹配,..,找d,目标串中没有d(不匹配),停止查找,返回d在str中的位置3

//find_last_not_of-与find_first_not_of相似,只不过查找顺序是从指定位置向前

cout << str.find_last_not_of("kiajbvehfgmlc", 6) << endl;//3
    return 0;
}

常用函数

构造函数

代码语言:javascript复制
 string strs //生成空字符串
string s(str) //生成字符串str的复制品
string s(str, stridx) //将字符串str中始于stridx的部分作为构造函数的初值
string s(str, strbegin, strlen) //将字符串str中始于strbegin、长度为strlen的部分作为字符串初值
string s(cstr) //以C_string类型cstr作为字符串s的初值
string s(cstr,char_len)    //以C_string类型cstr的前char_len个字符串作为字符串s的初值
strings(num, c) //生成一个字符串,包含num个c字符
strings(strs, beg, end)  
  //以区间[beg, end]内的字符作为字符串s的初值
  
  std::string s('x');    //错误
std::string s(1, 'x');    //正
std::string s("x");    //正确

析构函数

代码语言:javascript复制
~string() //销毁所有内存,释放内存

示例

代码语言:javascript复制
    #include <iostream>
    #include <string>
    using namespace std;
    int main ()
{
        string str ("12345678");
        char ch[ ] = "abcdefgh";
        string a; //定义一个空字符串
        string str_1 (str); //构造函数,全部复制
        string str_2 (str, 2, 5); //构造函数,从字符串str的第2个元素开始,复制5个元素,赋值给str_2
        string str_3 (ch, 5); //将字符串ch的前5个元素赋值给str_3
        string str_4 (5,'X'); //将 5 个 'X' 组成的字符串 "XXXXX" 赋值给 str_4
        string str_5 (str.begin(), str.end()); //复制字符串 str 的所有元素,并赋值给 str_5
        cout << str << endl;
        cout << a << endl ;
        cout << str_1 << endl;
        cout << str_2 << endl;
        cout << str_3 << endl;
        cout << str_4 << endl;
        cout << str_5 << endl;
        return 0;
    }

迭代器

代码语言:javascript复制
const_iterator begin()const;
iterator begin();                //返回string的起始位置
const_iterator end()const;
iterator end();                    //返回string的最后一个字符后面的位置
const_iterator rbegin()const;
iterator rbegin();                //返回string的最后一个字符的位置
const_iterator rend()const;
iterator rend();                    //返回string第一个字符位置的前面

示例

代码语言:javascript复制
    #include <iostream>
    #include <string>
    #include <algorithm>
    using namespace std;
    int main ()
{
        string s ("The zip code of Hondelage in Germany is 38108.");
        cout << "Original: " << s << endl;
        string sd(s.begin(),s.end ()); //构造函数中使用迭代器
        cout << "Destination: " << sd << endl;
      //  transform (sd.begin(), sd.end(), sd.begin(), toupper); //算法中使用迭代器(仿函数)
      //  cout << "Destination (All Toupper)): " << sd << endl;
        string sd1;
        sd1.append (sd.begin(),(sd.end() -7)); //append()函数中使用迭代器
        cout << "Destination sd1: " << sd1 << endl;
        string sd2;
        string::reverse_iterator iterA;
        string temp = "0";
        for (iterA = sd.rbegin (); iterA != sd.rend (); iterA  ) //reverse_iterator
        {
            temp=* iterA;
            sd2.append (temp);
        }
        cout << "Destination sd2: " << sd2 << endl;
        sd2.erase (0, 15); //erase()函数中使用迭代器
        cout << "Destination sd2 (Erased 15 chars) : " << sd2 << endl;
        string::iterator iterB = sd2.begin ();
        string sd3 = string ("12345678");
        sd2.insert (sd2.begin(), sd3.begin(), sd3.end()); //insert()函数中使用迭代器
        cout << "Destination sd2 (Insert 8 chars) : " << sd2 << endl;
        sd2.replace (sd2.begin (), sd2.end(), "This is an Exarrple of Replace"); //Replace
        cout <<"Destination sd2 (Replace All): " << sd2 << endl; // replace ()函数中使用迭代器
    }

字符长度

代码语言:javascript复制
int capacity()const;    //返回当前容量(即string中不必增加内存即可存放的元素个数)
int max_size()const;    //返回string对象中可存放的最大字符串的长度
int size()const;        //返回当前字符串的大小
int length()const;       //返回当前字符串的长度
bool empty()const;        //当前字符串是否为空
void resize(int len,char c);//把字符串当前大小置为len,并用字符c填充不足的部分

示例

代码语言:javascript复制
    #include <iostream>
    #include <string>
    using namespace std;
    int main ()
{
        int size = 0;
        int length = 0;
        unsigned long maxsize = 0;
        int capacity=0;
        string str ("12345678");
        string str_custom;
        str_custom = str;
        str_custom.resize (5);
        size = str_custom.size();
        length = str_custom.length();
        maxsize = str_custom.max_size();
        capacity = str_custom.capacity();
        cout << "size = " << size << endl;
        cout << "length = " << length << endl;
        cout << "maxsize = " << maxsize << endl;
        cout << "capacity = " << capacity << endl;
        return 0;
    }

输入流

代码语言:javascript复制
    string input("hello,this is a test");
    istringstream is(input);
    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()
//2
getline(istream &in,string &s); //用于从输入流in中读取字符串到s中,以换行符'n'分开
//eg:
string s1;
getline (cin, s1); // 实现了读取一行字符,包括空格、制表符、回车符等行内字符和符号,以n分开

示例

代码语言:javascript复制
    #include <iostream>
    #include <string>
    using namespace std;
   int main ()
{
    string s1, s2;
    getline(cin, s1);
    getline(cin, s2, ' ');
    cout << "You inputed chars are: " << s1 << endl;
    cout << "You inputed chars are: " << s2 << endl;
    }

访问元素

代码语言:javascript复制
const char &operator[](int n)const;
const char &at(int n)const;
char &operator[](int n);
char &at(int n);

示例

1

代码语言:javascript复制
    #include <iostream>
    #include <string>
    int main()
{
        const std::string cS ("c.biancheng.net");
        std::string s ("abode");
        char temp =0;
        char temp_1 = 0;
        char temp_2 = 0;
        char temp_3 = 0;
        char temp_4 = 0;
        char temp_5 = 0;
        temp = s [2]; //"获取字符 'c'
        temp_1 = s.at(2); //获取字符 'c'
        temp_2 = s [s.length()]; //未定义行为,返回字符'',但Visual C   2012执行时未报错
        temp_3 = cS[cS.length()]; //指向字符 ''
        //temp_4 = s.at (s.length ()); //程序异常
       // temp_5 = cS.at(cS.length ()); //程序异常
      std::cout << temp <<temp_1 << temp_2 << temp_3<<std::endl;
     // std::cout<< temp_4 << temp_5 << std::endl;
        return 0;
    }

2

代码语言:javascript复制
    #include <iostream>
    #include <string>
    int main()
{
        std::string s ("abode");
        std::cout << s << std::endl ;
        char& r = s[2] ; //建立引用关系
        char*p=&s[3]; //建立引用关系
        r='X' ;//修改内容
        *p='Y' ;//修改内容
        std::cout << s << std::endl; //输出
        s = "12345678"; //重新赋值
        r ='X'; //修改内容
        *p='Y'; //修改内容
        std::cout << s << std::endl; //输出
        return 0;
    }

字符串比较

代码语言:javascript复制

int compare(const string &s) const;//比较当前字符串和s的大小
int compare(int pos, int n,const string &s)const;//比较当前字符串从pos开始的n个字符组成的字符串与s的大小
int compare(int pos, int n,const string &s,int pos2,int n2)const;//比较当前字符串从pos开始n个字符字符串与s中pos2开始n2个字符字符串的大小
int compare(const char *s) const;
int compare(int pos, int n,const char *s) const;
int compare(int pos, int n,const char *s, int pos2) const;

示例

代码语言:javascript复制
#include <iostream>
#include <string>
using namespace std;
int main ()
{
    string A ("aBcdef");
    string B ("AbcdEf");
    string C ("123456");
    string D ("123dfg");
    //下面是各种比较方法
    int m=A.compare (B); //完整的A和B的比较
    int n=A.compare(1,5,B,4,2); //"Bcdef"和"AbcdEf"比较
    int p=A.compare(1,5,B,4,2); //"Bcdef"和"Ef"比较
    int q=C.compare(0,3,D,0,3); //"123"和"123"比较
    cout << "m = " << m << ", n = " << n <<", p = " << p << ", q = " << q << endl;
    cin.get();
}

删除

代码语言:javascript复制
iterator erase(iterator first, iterator last);//删除[first,last)之间的所有字符,返回删除后迭代器的位置
iterator erase(iterator it);//删除it指向的字符,返回删除后迭代器的位置
string &erase(int pos = 0, int n = npos);//删除pos开始的n个字符,返回修改后的字符串

赋值

代码语言:javascript复制
 basic_string& assign (const E*s); //直接使用字符串赋值
basic_string& assign (const E*s, size_type n);
basic_string& assign (const basic_string & str, size_type pos, size_type n);
//将str的子串赋值给调用串
basic_string& assign (const basic_string& str);    //使用字符串的“引用”賦值
basic_string& assign (size_type n, E c) ; //使用 n个重复字符賦值
basic_string& assign (const_iterator first, const_iterator last);    //使用迭代器赋值

示例

代码语言:javascript复制
    #include <iostream>
    #include <string>
    using namespace std;
    int main()
{
        string str1 ("123456");
        string str;
        str.assign (str1); //直接赋值
        cout << str << endl;
        str.assign (str1, 3, 3); //赋值给子串
        cout << str << endl;
        str.assign (str1,2,str1.npos);//赋值给从位置 2 至末尾的子串
        cout << str << endl;
        str.assign (5,'X'); //重复 5 个'X'字符
        cout << str << endl;
        string::iterator itB;
        string::iterator itE;
        itB = str1.begin ();
        itE = str1.end();
        str.assign (itB, (--itE)); //从第 1 个至倒数第 2 个元素,赋值给字符串 str
        cout << str << endl;
        return 0;
    }

插入

代码语言:javascript复制
string &insert(int p0, const char *s);
string &insert(int p0, const char *s, int n);
string &insert(int p0,const string &s);
string &insert(int p0,const string &s, int pos, int n);
//前4个函数在p0位置插入字符串s中pos开始的前n个字符
string &insert(int p0, int n, char c);//此函数在p0处插入n个字符c
iterator insert(iterator it, char c);//在it处插入字符c,返回插入后迭代器的位置
void insert(iterator it, const_iterator first, const_iterator last);//在it处插入[first,last)之间的字符
void insert(iterator it, int n, char c);//在it处插入n个字符c

示例

代码语言:javascript复制
    #include <iostream>
    #include <string>
    using namespace std;
    int main ()
{
        string str1 ("123456");
        string str2 ("abcdefghijklmn");
        string str;
        str.assign(str1);
        cout << str << endl;
        str.assign (str1 , 3, 3);
        cout << str << endl;
        str.assign (str1, 2, str1.npos);
        cout << str << endl;
        str.assign (5, 'X');
        cout << str << endl;
        string::iterator itB;
        string::iterator itE;
        itB = str1.begin ();
        itE = str1.end();
        str.assign (itB, (--itE));
        cout << str << endl;
        str = str1;
        cout << str << endl;
        str.erase(3);
        cout << str << endl;
        str.erase (str.begin (), str.end());
        cout << ":" << str << ":" << endl;
        str.swap(str2);
        cout << str << endl;
        string A ("ello");
        string B ("H");
        B.insert (1, A);
        cout << B << endl;
        A = "ello";
        B ='H';
        B.insert (1, "yanchy ", 3);
        cout << "插入: " << B << endl;
        A = "ello";
        B = "H";
        B.insert(1,A,2,2);
        cout << "插入:" << B << endl;
        A = "ello";
        B = "H";
        B.insert (1,5,'C');
        cout << "插入:" << B << endl;
        A = "ello";
        B = "H";
        string::iterator it = B.begin ()  1;
        const string::iterator itF = A.begin ();
        const string::iterator itG = A.end ();
        B.insert(it,itF,itG);
        cout<<"插入:"<< B << endl;
        A = "ello";
        B = "H";
        cout << "A = " << A <<", B = " << B << endl ;
        B.append (A);
        cout << "追加:" << B << endl;
        B = "H";
        cout << "A = "<< A << ", B= " << B << endl;
        B.append("12345", 2);
        cout << "追加:" << B << endl;
        A = "ello";
        B = "H";
        cout << "A = " << A << ", B= " << B << endl;
        B.append ("12345", 2, 3);
        cout << "追加:" << B << endl;
        A = "ello";
        B = "H";
        cout << "A = " << A <<", B = " << B << endl;
        B.append (10 , 'a');
        cout << "追加:"<< B << endl;
        A = "ello";
        B = "H";
        cout << "A = " << A << ", B = " << B << endl;
        B.append(A.begin() , A.end());
        cout << "追加:" << B << endl;
        cin.get();
        return 0;
    }

替换

代码语言:javascript复制
string &replace(int p0, int n0,const char *s);//删除从p0开始的n0个字符,然后在p0处插入串s
string &replace(int p0, int n0,const char *s, int n);//删除p0开始的n0个字符,然后在p0处插入字符串s的前n个字符
string &replace(int p0, int n0,const string &s);//删除从p0开始的n0个字符,然后在p0处插入串s
string &replace(int p0, int n0,const string &s, int pos, int n);//删除p0开始的n0个字符,然后在p0处插入串s中从pos开始的n个字符
string &replace(int p0, int n0,int n, char c);//删除p0开始的n0个字符,然后在p0处插入n个字符c
string &replace(iterator first0, iterator last0,const char *s);//把[first0,last0)之间的部分替换为字符串s
string &replace(iterator first0, iterator last0,const char *s, int n);//把[first0,last0)之间的部分替换为s的前n个字符
string &replace(iterator first0, iterator last0,const string &s);//把[first0,last0)之间的部分替换为串s
string &replace(iterator first0, iterator last0,int n, char c);//把[first0,last0)之间的部分替换为n个字符c
string &replace(iterator first0, iterator last0,const_iterator first, const_iterator last);//把[first0,last0)之间的部分替换成[first,last)之间的字符串

抽取

代码语言:javascript复制
string substr(int pos = 0,int n = npos) const;//返回pos开始的n个字符组成的字符串

示例

代码语言:javascript复制
    #include <iostream>
    #include <string>
    using namespace std;
    int main ()
{
        string var ("abcdefghijklmn");
        const string dest ("1234");
        string dest2 ("567891234");
        var.replace (3,3, dest);
        cout << "1: " << var << endl;
        var = "abcdefghijklmn";
        var.replace (3,1, dest.c_str(), 1, 3);
        cout << "2: " << var << endl;
        var ="abcdefghijklmn";
        var.replace (3, 1, 5, 'x');
        cout << "3: " << var << endl;
        string::iterator itA, itB;
        string::iterator itC, itD;
        itA = var.begin();
        itB = var.end();
        var = "abcdefghijklmn";
        var.replace (itA, itB, dest);
        cout << "4: " << var << endl;
        itA = var.begin ();
        itB = var.end();
        itC = dest2.begin ()  1;
        itD = dest2.end ();
        var = "abodefghijklmn";
        var.replace (itA, itB, itC, itD);
        cout << "5: " << var << endl;
        var = "abcdefghijklmn";
        var.replace (3, 1, dest.c_str(), 4); //这种方式会限定字符串替换的最大长度
        cout <<"6: " << var << endl;
        return 0;
    }

查找

代码语言:javascript复制
int find(char c, int pos = 0) const;//从pos开始查找字符c在当前字符串的位置
int find(const char *s, int pos = 0) const;//从pos开始查找字符串s在当前串中的位置
int find(const char *s, int pos, int n) const;//从pos开始查找字符串s中前n个字符在当前串中的位置
int find(const string &s, int pos = 0) const;//从pos开始查找字符串s在当前串中的位置
//查找成功时返回所在位置,失败返回string::npos的值
 
int rfind(char c, int pos = npos) const;//从pos开始从后向前查找字符c在当前串中的位置
int rfind(const char *s, int pos = npos) const;
int rfind(const char *s, int pos, int n = npos) const;
int rfind(const string &s,int pos = npos) const;
//从pos开始从后向前查找字符串s中前n个字符组成的字符串在当前串中的位置,成功返回所在位置,失败时返回string::npos的值
 
int find_first_of(char c, int pos = 0) const;//从pos开始查找字符c第一次出现的位置
int find_first_of(const char *s, int pos = 0) const;
int find_first_of(const char *s, int pos, int n) const;
int find_first_of(const string &s,int pos = 0) const;
//从pos开始查找当前串中第一个在s的前n个字符组成的数组里的字符的位置。查找失败返回string::npos
 
int find_first_not_of(char c, int pos = 0) const;
int find_first_not_of(const char *s, int pos = 0) const;
int find_first_not_of(const char *s, int pos,int n) const;
int find_first_not_of(const string &s,int pos = 0) const;
//从当前串中查找第一个不在串s中的字符出现的位置,失败返回string::npos
 
int find_last_of(char c, int pos = npos) const;
int find_last_of(const char *s, int pos = npos) const;
int find_last_of(const char *s, int pos, int n = npos) const;
int find_last_of(const string &s,int pos = npos) const;
int find_last_not_of(char c, int pos = npos) const;
int find_last_not_of(const char *s, int pos = npos) const;
int find_last_not_of(const char *s, int pos,  int n) const;
int find_last_not_of(const string &s,int pos = npos) const;
//find_last_of和find_last_not_of与find_first_of和find_first_not_of相似,只不过是从后向前查找

示例

1

代码语言:javascript复制
    #include <iostream>
    #include <string>
    using namespace std;
    int main ()
{
        string str_ch (" for");
        string str (" Hi, Peter, I'm sick. Please bought some drugs for me.");
        string::size_type m= str.find ('P', 5);
        string::size_type rm= str.rfind('P', 5);
        cout << "Example - find() : The position (forward) of 'P' is: " << (int) m << endl;
        cout << "Example - rfind(): The position (reverse) of 'P' is: " << (int) rm << endl;
        string::size_type n = str.find (" some", 0);
        string::size_type rn = str.rfind (" some", 0);
        cout << "Example - find () : The position (forward) of 'some' is: " << (int) n << endl;
        cout << "Example - rfind () : The position (reverse) of 'some' is: " << (int) rn << endl;
        string::size_type mo = str.find (" drugs", 0, 5);
        string::size_type rmo = str.rfind (" drugs", 0, 5);
        cout << "Example - find(): The position (forward) of 'drugs' is: " << (int) mo << endl;
        cout << "Example - rfind(): The position (reverse) of 'drugs' is: " << (int) rmo << endl;
        string::size_type no = str.find (str_ch, 0);
        string::size_type rno = str.rfind(str_ch, 0);
        cout << "Example - find (): The position of 'for' is: " << (int) no << endl;
        cout << "Example - rfind(): The position of 'for' is: " << (int) rno << endl;
        cin.get ();
    }

2

代码语言:javascript复制
    #include <iostream>
    #include <string>
    using namespace std;
    int main ()
{
        string str_ch ("for");
        string str("Hi, Peter, I'm sick. Please bought some drugs for me. ");
        int length = str.length();
        string::size_type m = str.find_first_of ('P', 0);
        string::size_type rm = str.find_last_of ('P', (length - 1));
        cout << "Example - find_first_of (): The position (forward) of 'P' is: " << (int) m << endl;
        cout << "Example - find_last_of (): The position (reverse) of 'P' is:" << (int) rm << endl;
        string:: size_type n = str.find_first_of ("some", 0);
        string:: size_type rn = str.find_last_of ("some", (length -1));
        cout << "Example - find_first_of(): The position (forward) of 'some' is: " << (int) n << endl;
        cout << "Example - find_last_of(): The position (reverse) of 'some' is: " << (int) rn << endl;
        string:: size_type mo = str.find_first_of ("drugs", 0, 5);
        string:: size_type rmo = str.find_last_of ("drugs", (length-1), 5);
        cout << "Example - find_first_of () : The position (forward) of 'drugs' is: " << (int) mo << endl;
        cout << "Example - find_last_of () : The position (reverse) of 'drugs' is: " << (int) rmo << endl;
        string::size_type no = str.find_first_of (str_ch, 0);
        string::size_type rno = str.find_last_of (str_ch, (length -1));
        cout << "Example - find_first_of() : The position of 'for' is: " << (int) no << endl;
        cout << "Example - find_last_of () : The position of 'for' is: " << (int) rno << endl;
        cin.get();
        return 0;
    }

3

代码语言:javascript复制
    #include <iostream>
    #include <string>
    using namespace std;
    int main ()
{
        string str_ch (" for");
        string str ("Hi, Peter, I'm sick. Please bought some drugs for me.");
        int length = str.length ();
        string::size_type m= str.find_first_not_of ('P',0);
        string::size_type rm= str.find_last_not_of ('P', (length -1));
        cout << "Example - find_first_of (): The position (forward) of 'P' is: " << (int) m << endl;
        cout << "Example - find_last_of (): The position (reverse) of 'P' is: " << (int) rm << endl;
        string:: size_type n = str.find_first_not_of ("some", 0);
        string:: size_type rn = str.find_last_not_of ("some", (length -1));
        cout << "Example - find_first_of (): The position (forward) of 'some' is: " << (int) n << endl;
        cout << "Example - find_last_of (): The position (reverse) of 'some' is: " << (int) rn << endl;
        string:: size_type mo = str.find_first_not_of ("drugs", 0, 5);
        string:: size_type rmo = str.find_last_not_of ("drugs", (length-1), 5);
        cout << "Example - find_first_of (): The position (forward) of 'drugs' is: " << (int) mo << endl;
        cout << "Example - find_last_of (): The position (reverse) of 'drugs' is: " << (int) rmo << endl;
        string::size_type no = str.find_first_not_of (str_ch, 0);
        string::size_type rno = str.find_last_not_of (str_ch, (length-1));
        cout << "Example - find_first_of (): The position of 'for' is: " << (int) no << endl;
        cout << "Example - find_last_of () : The position of 'for' is: " << (int) rno << endl;
        cin.get ();
        return 0;
    }

附加:

auto_ptr

引入:

代码语言:javascript复制
void f()
{
    Type* pt(new Type);
    //一些代码...
    delete pt;
}

 void f()
{
   auto_ptr<Type> pt(new Type);
   //一些代码...
}

同样的功能,auto_ptr省去了delete[]

示例:

代码语言:javascript复制
#include <iostream>
#include <memory>
using namespace std;

void f(auto_ptr<int> aptr)
{
    cout<<*aptr<<endl;
}
int main()
{
    auto_ptr<int> aptr(new int(10));
   f(aptr);
    cout<<*aptr<<endl;//错误,经过f函数调用,原有的aptr已经不再拥有任何对象了
     // get:返回auto_ptr指向的那个对象的内存地址
   int *p=new int(10);
   cout << "the adress of p: "<< p << endl;

   auto_ptr<int> aptr(p);
   cout << "the adress of aptr: " << &aptr << endl;
   cout << "the adress of the object which aptr point to: " 
       << aptr.get() << endl;
      
   // reset:重新设置auto_ptr指向的对象,
   // 类似于赋值操作,但是赋值操作不允许将一个普通指针直接赋值给auto_ptr,而reset允许
   auto_ptr<string> aptr(new string("name"));
   aptr.reset(new string("sex"));

   cout<<*(aptr.get())<<endl;
   cout<<*aptr<<endl;
  
   //release:返回auto_ptr指向的那个对象的内存地址,并且释放这个对象的所有权
   //用此函数初始化auto_ptr可以避免两个auto_ptr对象指向同一个对象的情况
    auto_ptr<string> aptr(new string("name"));
    auto_ptr<string> aptr1(aptr.get());//这是两个auto_ptr拥有同一个对象
    auto_ptr<string> aptr2(aptr.release());//release可以先释放所有权,这样就不会指向同一个对象

    return 0;
}

0 人点赞