解锁Facebook评论数据:fbcrawl评论爬虫实战指南
2026/8/5 14:13:08
#include <string> using namespace std; int main() { // 1. 默认构造(空字符串) string s1; // 2. 用C风格字符串(char*)初始化 string s2("hello"); string s3 = "world"; // 等价于上面 // 3. 拷贝构造(用另一个string初始化) string s4(s2); // s4 = "hello" string s5 = s2; // 等价 // 4. 用部分字符初始化 string s6("hello", 3); // 取前3个字符 → "hel" string s7(s2, 1, 3); // 从s2的索引1开始,取3个 → "ell" // 5. 用重复字符初始化 string s8(5, 'a'); // 5个'a' → "aaaaa" // 6. C++11+:初始化列表 string s9{'h', 'e', 'l', 'l', 'o'}; return 0; }string s; // 1. 直接赋值 s = "hello"; s = string("world"); s = 'a'; // 单个字符也可以 // 2. assign():更灵活的赋值 s.assign("hello"); // 直接赋值 s.assign("hello", 3); // 取前3个 → "hel" s.assign(5, 'x'); // 5个'x' → "xxxxx" s.assign(s2, 1, 2); // 从s2索引1取2个 // 3. swap():交换两个字符串的内容 string a = "apple", b = "banana"; a.swap(b); // 现在a="banana", b="apple"string s = "hello"; // 1. [] 下标访问(不检查越界,越界会崩溃) char c1 = s[0]; // 'h' s[0] = 'H'; // 修改为 "Hello" // 2. at():带越界检查的访问(越界会抛异常,更安全) char c2 = s.at(1); // 'e' // 3. 首尾字符 char first = s.front(); // C++11+,等价 s[0] char last = s.back(); // C++11+,等价 s[s.size()-1] // 4. 迭代器遍历(类似指针) for (auto it = s.begin(); it != s.end(); ++it) { cout << *it; // 依次输出 h e l l o } // 反向迭代器 for (auto it = s.rbegin(); it != s.rend(); ++it) { cout << *it; // 依次输出 o l l e h }string s1 = "hello", s2 = "world"; // 1. + 运算符(最直观) string s3 = s1 + " " + s2; // "hello world" s1 += "!"; // s1 变成 "hello!" // 2. append():更灵活的拼接 s1.append(s2); // 直接拼s2 s1.append("abc", 2); // 拼"abc"的前2个 → "ab" s1.append(3, 'x'); // 拼3个'x' → "xxx" s1.append(s2, 1, 2); // 拼s2从索引1开始的2个字符 // 3. push_back():只拼单个字符 s1.push_back('!');string a = "apple", b = "banana"; // 1. 直接用比较运算符(按字典序/ASCII码比较) if (a == b) cout << "相等"; if (a != b) cout << "不等"; if (a < b) cout << "a字典序在b前面"; // "apple" < "banana" 为真 if (a > b) cout << "a字典序在b后面"; if (a <= b) cout << "小于等于"; if (a >= b) cout << "大于等于"; // 2. compare():更详细的比较(返回int) // 返回0:相等;返回<0:a < b;返回>0:a > b int res = a.compare(b); res = a.compare(1, 3, b); // a从索引1开始的3个字符 和 b 比较 res = a.compare(1, 3, b, 0, 2); // a的[1,3) 和 b的[0,2) 比较这是你处理网络请求最常用的操作,比如解析 URI、找参数位置。
string s = "hello world hello"; // 1. find():从前往后找,返回第一次出现的索引(找不到返回 string::npos) size_t pos = s.find("hello"); // 返回 0 pos = s.find("hello", 1); // 从索引1开始找,返回 12 pos = s.find('w'); // 找单个字符,返回 6 pos = s.find("abc"); // 找不到,返回 string::npos // 2. rfind():从后往前找(反向查找) pos = s.rfind("hello"); // 从后往前找,返回 12 // 3. find_first_of():找任意一个匹配的字符 pos = s.find_first_of("aeiou"); // 找第一个元音字母,返回 1('e') // 4. find_last_of():找最后一个匹配的字符 pos = s.find_last_of("aeiou"); // 返回 13(最后一个'l'前面的'o') // 5. find_first_not_of():找第一个不匹配的字符 pos = s.find_first_not_of("helo");// 找第一个不是h/e/l/o的字符,返回5(空格) // 【常用判断】找不到的处理 if (pos == string::npos) { cout << "没找到"; }string s = "hello world"; // substr(pos, len):从pos开始,截取len个字符 string sub1 = s.substr(0, 5); // 从0开始取5个 → "hello" string sub2 = s.substr(6); // 从6开始取到末尾 → "world" string sub3 = s.substr(6, 3); // 从6开始取3个 → "wor"string s = "hello world"; // replace(pos, len, new_str):从pos开始,替换len个字符为new_str s.replace(6, 5, "C++"); // 把"world"换成"C++" → "hello C++" // 更灵活的替换 s.replace(0, 5, "hi", 2); // 替换前5个为"hi"的前2个string s = "hello"; // insert(pos, str):在pos位置插入str s.insert(5, " world"); // 在索引5插入 → "hello world" s.insert(0, 2, 'x'); // 在开头插入2个'x' → "xxhello world"string s = "hello world"; // erase(pos, len):从pos开始删除len个字符 s.erase(5, 6); // 删除从5开始的6个 → "hello" s.erase(0); // 从0删到末尾 → 空字符串 s.clear(); // 清空字符串(等价 s.erase()) // 用迭代器删除 s.erase(s.begin()); // 删除第一个字符 s.erase(s.begin(), s.begin()+3); // 删除前3个字符string s = "hello"; // string → const char*(只读,不要修改返回值) const char* c_str = s.c_str(); const char* data = s.data(); // C++11前和c_str略有不同,C++11后等价 // char* → string(直接赋值或构造即可) char arr[] = "world"; string s2(arr);// 【数字 → string】 string s; s = to_string(123); // int → "123" s = to_string(45.67); // double → "45.670000" s = to_string(true); // bool → "1" // 【string → 数字】 string num_str = "123"; int i = stoi(num_str); // string → int long l = stol(num_str); // string → long long long ll = stoll(num_str); // string → long long float f = stof("3.14"); // string → float double d = stod("3.14159"); // string → double // 进阶:带进制和位置参数 size_t pos; int hex = stoi("0x1a", &pos, 16); // 16进制转10进制 → 26string s = "hello"; // 1. 大小/长度 if (s.empty()) cout << "空字符串"; int len = s.size(); // 等价 s.length(),推荐用size()(和STL容器统一) // 2. 调整大小 s.resize(10, 'x'); // 把s调整为10个字符,多出来的用'x'填充 → "helloxxxxx" s.resize(3); // 截断为3个字符 → "hel" // 3. 预留空间(优化性能,避免频繁扩容) s.reserve(100); // 预留100个字符的空间,但不改变size() int cap = s.capacity(); // 获取当前容量 // 4. C++17+:string_view(只读视图,零拷贝,性能优化) // 适合只需要读取、不需要修改字符串的场景,避免拷贝开销 string_view sv = s; // 直接指向s的内存,不拷贝 cout << sv[0]; // 读取和string一样string req_line = "GET /index.html HTTP/1.1"; size_t space1 = req_line.find(' '); size_t space2 = req_line.find(' ', space1 + 1); string method = req_line.substr(0, space1); // "GET" string uri = req_line.substr(space1 + 1, space2 - space1 - 1); // "/index.html" string version = req_line.substr(space2 + 1); // "HTTP/1.1"//更好的方法 std::string req_line = "GET /index.html HTTP/1.1"; std::string method, uri, version; std::istringstream iss(req_line); iss >> method >> uri >> version;string trim(string s) { // 去除开头空格 s.erase(0, s.find_first_not_of(" \t\n\r")); // 去除末尾空格 s.erase(s.find_last_not_of(" \t\n\r") + 1); return s; }vector<string> split(const string& s, char delimiter) { vector<string> tokens; string token; istringstream tokenStream(s); while (getline(tokenStream, token, delimiter)) { tokens.push_back(token); } return tokens; } // 使用:split("a,b,c", ',') → 返回 {"a","b","c"}