linux網(wǎng)站如何做ip解析一個新公眾號怎么吸粉
引言
針對C++中的string,本文主要講解如何對其進行插入、刪除、查找、比較、截斷、分割以及與數(shù)字之間的相互轉(zhuǎn)換等。
字符串插入
1. append方法
std::string str = "hello";
str.append(7, 'w'); // 在末尾添加7個字符'w'
str.append("wwwwwww"); // 在末尾添加一個字符串
- 兩個參數(shù):第一個是字符個數(shù),第二個是目標(biāo)字符
- 一個參數(shù):要拼接的字符串
- 返回值是對象的引用,也就是可以有如下用法:
std::string str;
str.append(1, 'c').append("abc").append('d'); // str="cabcd"
2. 通過加法來拼接
std::string str = "hello";
str += " world"; // 第一種加法
// str = str + "world"; // 第二種加法
簡單比較下兩種加法的開銷:
#include <string>
#include <iostream>
#include <chrono>
void costTime(void(*f)(), const std::string& info)
{auto beforeTime = std::chrono::steady_clock::now();f();auto afterTime = std::chrono::steady_clock::now();double duration_millsecond = std::chrono::duration<double, std::milli>(afterTime - beforeTime).count();std::cout << info << " total cost " << duration_millsecond << "ms" << std::endl;
}int main () {costTime([](){std::string str;for (int i = 0; i < 10000; ++i) {str += "wwwwwww";} }, "+=");costTime([](){std::string str;for (int i = 0; i < 10000; ++i) {str = str + "wwwwwww";} }, "=");return 0;
}
執(zhí)行結(jié)果:
+= total cost 0.167382ms
= total cost 128.886ms
由此可見,+=的方式效率要高出許多,所以在需要考慮這類性能的時候,盡量能使用+=就盡量使用。至于其中的原因:簡單理解為,=的方式每次賦值前都需要創(chuàng)建一個和str幾乎等同的(空間上)臨時對象,也就是str的空間越大,隨之而來的創(chuàng)建臨時對象所需的時間就會越長,最終導(dǎo)致效率極低。
3. push_back方法
這個方法就比較簡單了,就是在尾部追加一個字符,就不做介紹了。
4. insert方法
std::string str = "abc";
// 在指定位置插入字符串
str.insert(2, "ef");
assert("abefc" == str);// 在指定位置插入多個字符
str.insert(0, 3, 'x');
assert("xxxabefc" == str);// 在指定位置(迭代器)插入字符或多個字符
str.insert(str.begin() + 1, 'y');
str.insert(str.begin(), 2, 't');
assert("ttxyxxabefc" == str);// 在指定位置插入字符串的指定子串
// 插入的是從1的位置開始的3個字符
str.insert(0, "hgjkr", 1, 3);
assert("gjkttxyxxabefc" == str);
字符串刪除
std::string str = "abcdefght";
// 從指定位置開始刪除指定長度的字符
str.erase(2, 2);
assert("abefght" == str);// 從指定位置刪除至末尾
str.erase(4);
assert("abef" == str);// 從指定位置(迭代器)刪除至末尾
str.erase(str.begin() + 2);
assert("ab" == str);// 從指定開始位置(迭代器)刪除至指定結(jié)束位置(迭代器)
// 左閉右開
str.erase(str.begin() + 1, str.end());
assert("a" == str);
字符串查找
1. find方法
std::string str = "abc bdef";
// 從左向右查找目標(biāo)字符第一次出現(xiàn)的位置下標(biāo)
int pos = str.find('b');
assert(pos == 1);
// 從左向右查找目標(biāo)字符串第一次出現(xiàn)的位置下標(biāo)
pos = str.find("bd");
assert(pos == 4);// 從指定位置開始查找目標(biāo)字符或字符串
pos = str.find('b', 2);
assert(pos == 4);
如果目標(biāo)字符或字符串不存在,則返回std::string::n_pos
;
簡單測試下按字符查找和按字符串查找的效率:
int main() {costTime([](){std::string str = "abcdefgtbxyx";for (int i = 0; i < 100000; ++i) {str.find("x");} }, "str");costTime([](){std::string str = "abcdefgtbxyx";for (int i = 0; i < 100000; ++i) {str.find('x');} }, "char");
}
耗時如下,可以看出,同樣查找x的位置,按字符的方式查找要比按字符串的方式查找更快。
str total cost 3.22321ms
char total cost 0.250851ms
2. rfind方法
參數(shù)同find,區(qū)別在于它的查找方向是從右向左。
std::string str = "abc bdef";
int pos = str.rfind('b');
assert(pos == 4);
3. find_first_of/find_first_not_of
std::string str = "abcfbcg";
// 從左向右查找第一次出現(xiàn)在目標(biāo)字符串中的字符
// 字符c存在于目標(biāo)字符串'fcg',所以返回pos=2
int pos = str.find_first_of("fcg");
assert(pos == 2);
對于pos = find_first_of("fcg")
可以簡單理解為:
unsigned int pos1 = str.find('f');
unsigned int pos2 = str.find('c');
unsigned int pos3 = str.find('g');
unsigned int pos = std::min(pos1, pos2, pos3);
而find_first_not_of則正好相反,查找第一次不包含目標(biāo)字符的位置。
std::string str = "abcdef";
int pos = str.find_first_not_of('abc');
assert(pos == 3);
4. find_last_of/find_last_not_of
使用方式同find_first_of/find_first_not_of,只不過查找方向是從右向左
std::string str = "abccfg";
int pos = str.find_last_of('cab');
assert(pos == 3);pos = str.find_last_not_of('cgf');
assert(pos == 1);
字符串替換
字符串替換使用replace
接口
std::string str = "hello world";
// 指定位置,指定長度的子串替換為目標(biāo)字符串
// 從5的位置長度為1的子串(空格)替換為'::'
str.replace(5, 1, "::");
assert("hello::world" == str);// 指定位置,指定長度的子串替換為指定數(shù)目的目標(biāo)字符
// 從5的位置長度為2的子串(::)替換為2個字符'!'
str.replace(5, 2, 2, '!');
assert("hello!!world" == str);// 指定位置,指定長度的子串替換為目標(biāo)字符串中指定位置的子串
// 從5的位置長度為2的子串(!!)替換為目標(biāo)串3的位置長度為2的子串'..'
str.replace(5, 2, "hjk..kl", 3, 2);
assert("hello..world" == str);// 替換為目標(biāo)串0的位置開始長度為2子串'??'
// 此重載方法對于參數(shù)為const char*類型結(jié)果如下
str.replace(5, 2, "??##", 2);
assert("hello??world" == str);// 替換為目標(biāo)串中2的位置開始到末尾的子串'##'
// 此重載方法對于參數(shù)為string類型的結(jié)果如下
std::string str2 = "??##";
str.replace(5, 2, str2, 2);
assert("hello##world" == str);// 迭代器起始位置到結(jié)束位置(不包含結(jié)束)替換為目標(biāo)串
str.replace(str.begin() + 5, str.begin() + 7, "$$");
assert("hello$$world" == str);
如果想要實現(xiàn)python中替換目標(biāo)字符或字符串的那種方式,得配合find方法,先找到目標(biāo)串的位置,然后再進行替換。當(dāng)然為了提高效率建議還是自己寫一個。
字符串截取
std::string str = "abcdef";
// 從2的位置開始截取到末尾
std::string str2 = str.substr(2);
assert(str2 == "cdef");// 從0的位置開始截取長度為2的子串
str2 = str.substr(0, 2);
assert(str2 == "ab");
注意substr
不改變調(diào)用者自身,它會返回一個新的副本,也就是str始終沒變。
字符串比較
std::string a = "abcd";
std::string b = "bcd";// 相等或者不相等可以直接通過==或!=來比較
assert(a != b);// 字符串a(chǎn)與b比較
int ret = a.compare(b);
assert(ret == -1);// 字符串a(chǎn)從1開始長度為3的子串與b比較
ret = a.compare(1, 3, b);
assert(ret == 0);// 字符串b從0開始長度為3的子串與a從1開始長度為3的子串比較
ret = b.compare(0, 3, a, 1, 3);
assert(ret == 0);
返回值有三種:1表示大于,-1表示小于,0為相等。
字符串與數(shù)字相互轉(zhuǎn)換
1. 字符串轉(zhuǎn)化為數(shù)字
stoi(const std::string& str, std::size_t* pos = nullptr, int base = 10)
- str:目標(biāo)字符串
- pos:傳出參數(shù),用于接收最后一個非數(shù)字的字符位置
- base:進制,默認10進制,如果為0,則按照字符串格式進行解析
只傳一個參數(shù)時:
std::string str = "123";
int num = std::stoi(str);
assert(num == 123);str = "123a";
num = std::stoi(str);
assert(num == 123);str = "a123";
// 此處會拋出異常invalid_argument
num = std::stoi(str);str = "12345678901";
// 會拋出異常out_of_range
num = std::stoi(str);
傳入pos:
std::string str = "111ab1";
size_t pos = 0;
int num = std::stoi(str, &pos);
assert(num == 111 && pos == 3);str = "123";
num = std::stoi(str, &pos);
assert(num == 123 && pos == str.size());
- 可以看出,如果需要判斷字符串是否完全是由數(shù)字組成,可以通過
pos == str.size()
來判斷。
傳入base:
std::string str = "111";
int num = std::stoi(str, nullptr, 2);
assert(num == 111b); // 二進制
num = std::stoi(str, nullptr, 8);
assert(num == 0111); // 八進制
num = std::stoi(str, nullptr, 16);
assert(num == 0x111); // 十六進制
num = std::stoi(str, nullptr, 0);
assert(num == 111);str = "0111";
num = std::stoi(str, nullptr, 0);
assert(num == 0111); // 8進制str = "0x111";
num = std::stoi(str, nullptr, 0);
assert(num == 0x111); // 16進制
其他轉(zhuǎn)換方法:
轉(zhuǎn)換方法 | 返回類型 |
---|---|
stol | long |
stoll | long long |
stoul | unsigned long |
stoull | unsigned long long |
stof | float |
stod | double |
stold | long double |
- 上述返回值為無符號整型時,如果傳入的字符串是有符號的,依然會正常轉(zhuǎn)換,只不過轉(zhuǎn)換的結(jié)果是有符號數(shù)的無符號形式,比如-1的無符號形式是0xFFFFFFFF,對于32位整型而言。
- 對于浮點型轉(zhuǎn)換,是沒有進制這個參數(shù)的。
2. 數(shù)字轉(zhuǎn)化為字符串
數(shù)字轉(zhuǎn)化為字符串就相對簡單了,使用std::to_string
即可,參數(shù)可以是任何數(shù)字類型。
字符串分割
分割的方法有很多:
- 通過find方法查找目標(biāo)分隔符,然后利用substr去截取;
- 通過c函數(shù)strtok或strtok_r進行分割;
- 通過stringstream流。
下面只介紹其中一種,也就是stringstream流的方式:
#include <sstream>
#include <string>
#include <iostream>int main() {std::string str = " hello world I love you ";std::stringstream sstream(str);std::string buf;int cnt = 0;// 以空格分割while(std::getline(sstream, buf, ' ')) {if (buf.empty()) continue;std::cout << cnt++ << ":" << buf << std::endl;buf.clear();}return 0;
}
執(zhí)行結(jié)果如下:
0:hello
1:world
2:I
3:love
4:you
注意一定要判斷buf為空這種情況,否則第一次讀到空格時,buf將會是一個空值。