STL string常用函数

    xiaoxiao2026-08-26  5

    很详细的string api

    一.使用包含

    #include <string> using namespace std;

    二.声明

    string a = “hello”;

    三.增

    string类重载运算符operator>>用于输入,重载运算符operator<<用于输出操作 函数getline(istream &in,string &s)

    cin>>a; cin.ignore(); getline(cin,str); //从输入流中获取一行

    运算符+= + 连接字符串或字符,+运算符注意需要左边为字符串,右边可为字符 append() 连接到串尾

    std::string str; std::string str2="Writing "; std::string str3="print 10 and then 5 more"; // used in the same order as described above: str.append(str2); // "Writing " str.append(str3,6,3); // "10 " str.append("dots are cool",5); // "dots " str.append("here: "); // "here: " str.append(10u,'.'); // ".........." str.append(str3.begin()+8,str3.end()); // " and then 5 more" str.append<int>(5,0x2E); 结果:

    四.删

    erase() 删除指针指向的数据项

    string str ("This is an example phrase."); string::iterator it; str.erase (10,8); // "This is an phrase." //删除从10位置开始的8个字符 it=str.begin()+9; str.erase (it); // "This is a phrase." //删除9号位置字符 str.erase (str.begin()+5, str.end()-7); // "This phrase." // 删除从5号位置(i)开始,到-7号位置(p)的字符前一个字符,即“is a ”。记忆时可以str.erase (str.begin(), str.end());删除整个字符串,从中可以看出str.end()位置是字符串尾部再滞后一位。

    删除空格:

    删除首尾: if( !s.empty() ) { s.erase(0,s.find_first_not_of(" ")); s.erase(s.find_last_not_of(" ") + 1); } 删除所有: int index = 0; if( !s.empty()) { while( (index = s.find(' ',index)) != string::npos) { s.erase(index,1); } }

    五.改

    assign() 类似于等号进行赋值

    std::string str; std::string base="The quick brown fox jumps over a lazy dog."; // used in the same order as described above: str.assign(base); std::cout << str << '\n'; str.assign(base,10,9); std::cout << str << '\n'; // "brown fox" str.assign("pangrams are cool",7); std::cout << str << '\n'; // "pangram" str.assign("c-string"); std::cout << str << '\n'; // "c-string" str.assign(10,'*'); std::cout << str << '\n'; // "**********" str.assign<int>(10,0x2D); std::cout << str << '\n'; // "----------" str.assign(base.begin()+16,base.end()-12); std::cout << str << '\n'; // "fox jumps over" 结果: The quick brown fox jumps over a lazy dog. brown fox pangram c-string ********** ---------- fox jumps over

    insert(pos, s) 在pos处插入字符串s,也就是说插入的s在原来pos位置字符之前。

    replace() 替换字符串

    str = "0123456789" str.replace(str.find("0"), 1, ""); //从第一个0位置替换第一个0为空,1表示替换长度,输出:"1123456789" str.replace(str.begin(), str.begin()+2, “@@”); //用“@@”替换从begin位置开始的2个字符,输出:"@@23456789",注意str.begin()+2位置没有被替换 str.replace(0, 5, “abcdef”, 0, 3); //用“abcdef”的指定子串(从0位置共3个字符)替换从05位置上的str,输出:"abc56789" str.replace(0, 5, 3, 'c'); //用重复3次的c字符替换从指定位置0长度为5的内容 ,输出: "ccc56789"

    六.查

    [ ]和at() 均返回当前字符串中第n个字符的位置,但at函数提供范围检查,当越界时会抛出out_of_range异常,下标运算符[]不提供检查访问 find(c, pos) 从pos开始查找字符或字符串c在当前字符串的位置,find返回值为-1,需注意直接使用while时-1是为真。

    const string strSrc = "abcdefghijklmnopqrstuvwxyz"; pStr= "cdefppp"; pos = strSrc.find(pStr, 0, 4); // pos = 2 在strSrc中从0开始搜索pstr的前4个字符

    rfind(c, pos) 从pos开始从后向前查找字符c在当前串中的位置

    const string strSrc = "abcdefghijklmnopqrstuvwxyz"; strDst= "uvw"; pos = strSrc.rfind(strDst); // pos=20,字符u所在位置 pos = strSrc.rfind(p, 19, 2); // pos = -1,没有找到,从19号位置往前搜索

    find_first_of() 查找目标中任意一个字符在字符串中首次出现位置。 find_first_not_of() 查找目标中任意一个字符在字符串中首次不匹配位置 find_last_of()从源字符串起始位置pos(默认为npos)处,依此查找每个字符。如果某字符在目标字符串str(string型字符串)中,则返回首次匹配的该字符的位置。 find_last_not_of从源字符串起始位置pos(默认为npos)处,依此查找每个字符。如果某字符不在目标字符串str(string型字符串)中,则返回首次不匹配的该字符的位置。

    string strSrc = "abcdefghijklmnopqrstuvwxyz"; strDst= "cde"; pos = strSrc.find_first_of(strDst); // pos = 2 也就是说在strSrc中搜索c,d或者e,出现的第一个字符位置,从0开始搜首先遇到的是c字符,且c字符在strDst中。

    substr(pos,n) 截取从pos开始的n个字符组成的字符串

    string s("I love you!") ,sub; sub=s.substr(2,4); // sub= "love"

    capacity() 当前容量 max_size() string对象中可存放的最大字符串的长度 size() 当前字符串的大小 length() 当前字符串的长度 empty() 当前字符串是否为空

    七.遍历

    for(string::iterator it=a.begin(); it!=a.end(); it++){ *it; } for(int i=0; i<a.size(); i++){ a[i]; }

    八.其他

    string::size_type / string::npos : 在string中有一个特许类型string::size_type,用于弥补string在设计上的缺陷。比如string中find返回的类型就是string::size_type,在string中size_type默认为unsigned int。

    string a= "abcdefg "; string::size_type id; id = a.find( "h "); if(id != string::npos) // string::npos实际值为-1,表示没有搜索到目标字符串 { }

    字符串与数字连接: 原文 具体api

    #include <iostream> #include <string> #include <cstdlib> #include <sstream> using namespace std; //第一种C风格的转化(以前一直喜欢的 sprintf 功能强大) void test() { char *s = "dong"; int a = 52; float b = .1314; char *buf = new char[strlen(s) + sizeof(a) + 1]; sprintf(buf, "%s%d%.4f", s, a, b); printf("%s\n", buf); } //半C半C++风格 void test1() { string s = "dong"; int a = 520; char *buf = new char[10];//2147483647 int最大值 _itoa(a, buf, 10); //itoa虽然可以转化为各种进制,但是注意不能是float或者double s += buf; cout << s << "\t"; _itoa(a, buf, 16); s += buf; cout << s << endl; } //纯C++风格 void test2() { string s = "陈明东"; int a = 520; double b = .1314; ostringstream oss; oss << s << a << b << endl; cout << oss.str() << endl; } int main() { test(); test1(); test2(); return 0; } /* dong520.1314 dong520 dong520208 陈明东5200.1314 */
    转载请注明原文地址: https://ju.6miu.com/read-1311575.html
    最新回复(0)