一.字符串分割

有多种方法比如自己双指针实现split函数,这里主要说明当split为类单字符时候,如何快速用stringstream数据流去比较方便地处理,用法就是stringstream ss后getline的第一个参数改为ss而不是cin,然后第三个参数改为需要的分隔符如逗号

//具体看例子就懂,以下是将 “1:2,3:4,5:6” 存入vector<std::pair<int, int>>也就是{{1,2},{3,4},{5,6}}的代码,允许stringstream嵌套的使用

#include <iostream>
#include <sstream>
#include <vector>
#include <string>
#include <utility>

// 函数:将输入字符串分割成vector<pair<int, int>>
std::vector<std::pair<int, int>> splitStringToPairVector(const std::string& input) {
    std::vector<std::pair<int, int>> result;
    std::stringstream ss(input);
    std::string item;
    
    // 通过逗号分割每个pair
    while (std::getline(ss, item, ',')) {
        std::stringstream pairStream(item);
        std::string key, value;
        
        // 通过冒号分割key和value
        if (std::getline(pairStream, key, ':') && std::getline(pairStream, value, ':')) {
            result.emplace_back(std::stoi(key), std::stoi(value));
        }
    }
    
    return result;
}

int main() {
    std::string input;
    
    // 从标准输入获取整个字符串
    std::cout << "Enter the input string: ";
    std::getline(std::cin, input);

    // 将字符串分割为vector<pair<int, int>>
    std::vector<std::pair<int, int>> result = splitStringToPairVector(input);

    // 打印结果
    for (const auto& p : result) {
        std::cout << "(" << p.first << ", " << p.second << ")\n";
    }

    return 0;
}

有的时候分隔符为字符串,那就可以用int index = s.find(“子串”)进行分割

#include <iostream>
#include <vector>
#include <string>

std::vector<std::string> split(const std::string &s, const std::string &delimiter) {
    std::vector<std::string> tokens;
    size_t start = 0;
    size_t end = s.find(delimiter);

    while (end != std::string::npos) {
        tokens.push_back(s.substr(start, end - start));
        start = end + delimiter.length();
        end = s.find(delimiter, start);
    }

    tokens.push_back(s.substr(start)); // Add the last token
    return tokens;
}

int main() {
    std::string s = "aa|||aac|||cbb";
    std::string delimiter = "|||";

    std::vector<std::string> result = split(s, delimiter);

    for (const std::string &str : result) {
        std::cout << str << std::endl;
    }

    return 0;
}

二.常见数据类型转换

string 和 int/double/long long 的相互转换:

#include <string>
#include <iostream>

int main() {
    std::string str = "12345";
    int intValue = std::stoi(str);
    double doubleValue = std::stod(str);
    long long longLongValue = std::stoll(str);

    std::cout << "int: " << intValue << "\n";
    std::cout << "double: " << doubleValue << "\n";
    std::cout << "long long: " << longLongValue << "\n";

    return 0;
}
#include <string>
#include <iostream>

int main() {
    int intValue = 12345;
    double doubleValue = 12345.678;
    long long longLongValue = 123456789012345LL;

    std::string strInt = std::to_string(intValue);
    std::string strDouble = std::to_string(doubleValue);
    std::string strLongLong = std::to_string(longLongValue);

    std::cout << "string from int: " << strInt << "\n";
    std::cout << "string from double: " << strDouble << "\n";
    std::cout << "string from long long: " << strLongLong << "\n";

    return 0;
}

int 和 bitset 的相互转换

#include <bitset>
#include <iostream>

int main() {
    int num = 29;
    std::bitset<8> bits(num);

    std::cout << "Bitset: " << bits << "\n";

    return 0;
}
#include <bitset>
#include <iostream>

int main() {
    std::bitset<8> bits("00011101");
    unsigned long num = bits.to_ulong();
    std::cout << "Integer: " << int(num) << "\n";
    return 0;
}

三.进制转换

有的题会考任意的进制转换 2-16进制

一个任意进制的string转为int

#include <iostream>
#include <string>
#include <cmath>

int charToValue(char c) {
    if (c >= '0' && c <= '9')
        return c - '0';
    else if (c >= 'A' && c <= 'Z')
        return c - 'A' + 10;
    else if (c >= 'a' && c <= 'z')
        return c - 'a' + 10;
    return -1; // 无效字符
}

int baseToDecimal(const std::string& num, int base) {
    int decimal = 0;
    int length = num.size();

    for (int i = 0; i < length; ++i) {
        int value = charToValue(num[i]);
        if (value >= base) {
            std::cerr << "Invalid digit in number for the given base.\n";
            return -1;
        }
        decimal += value * std::pow(base, length - i - 1);
    }

    return decimal;
}

int转一个任意进制的string

#include <iostream>
#include <string>
#include <algorithm>

std::string intToBase(int num, int base) {
    if (base < 2 || base > 36) {
        std::cerr << "Base out of range (2-36).\n";
        return "";
    }

    if (num == 0)
        return "0";

    std::string chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    std::string result;
    bool isNegative = num < 0;

    if (isNegative) {
        num = -num;
    }

    while (num > 0) {
        result.push_back(chars[num % base]);
        num /= base;
    }

    if (isNegative) {
        result.push_back('-');
    }

    std::reverse(result.begin(), result.end());

    return result;
}

int main() {
    int number = 255;
    int base = 16;
    std::string baseString = intToBase(number, base);
    std::cout << "Number in base " << base << ": " << baseString << std::endl;
    return 0;
}

四.二进制的位运算

有的题会考位运算的track,以下是一些常用位运算结论

#include <iostream>

// 清零最低位的 1
int clearLowestOne(int n) {
    return n & (n - 1);
}

// 得到最低位的 1
int getLowestOne(int n) {
    return n & -n;
}

// 交换两个数
void swap(int &a, int &b) {
    a ^= b;
    b ^= a;
    a ^= b;
}

// 计算二进制中 1 的个数
int countOnes(int n) {
    int count = 0;
    while (n) {
        n = n & (n - 1);
        count++;
    }
    return count;
}

int main() {
    int a = 12; // 1100
    int b = 3;  // 0011

    // 清零最低位的 1
    std::cout << "Clear lowest 1 of " << a << ": " << clearLowestOne(a) << std::endl;

    // 得到最低位的 1
    std::cout << "Lowest 1 of " << a << ": " << getLowestOne(a) << std::endl;

    // 交换两个数
    std::cout << "Before swap: a = " << a << ", b = " << b << std::endl;
    swap(a, b);
    std::cout << "After swap: a = " << a << ", b = " << b << std::endl;

    // 计算二进制中 1 的个数
    std::cout << "Number of 1s in " << a << ": " << countOnes(a) << std::endl;

    return 0;
}

五.最大公约数和最小公倍数

辗转相除法

int gcd(int a, int b) { return b == 0 ? a : gcd(b, a % b); }

int lcm(int a, int b) { return a / gcd(a, b) * b; }