C++ OJ常用语法

VSCODE code runner设置

executerMap中:

"cpp": "cd $dir && g++ $fileName -o $fileNameWithoutExt.exe && ./$fileNameWithoutExt.exe && rm ./$fileNameWithoutExt.exe",

字符串

cin输入字符串

string a;
getline(cin,a);
cin.get(a,50);
cin.getline(a,50);

字符串分割

#include<sstream>
#include<string>
//1、stringstream 或者istringstream
vector<string> substrings;
istringstream input(str);   //读取str到字符串流中
string temp;
char pattern = ','; 
//使用getline函数从字符串流中读取,遇到分隔符时停止,和从cin中读取类似
//注意,将空格作为分割符时,getline会把空格转换为空串,建议使用方法2
while(getline(input, temp, pattern)){
    substrings.push_back(temp);
}
//2、istringstream,分割空格
string word;
istringstream ss(str);
while (ss >> word) {
    //do something
}

//3.字符串转vector<char>

std::string str = "Hello";
std::vector<char> charVector;

for (char c : str) {
    charVector.push_back(c);
}
// vector转字符串

// 1. char数组
std::vector<char> vec = {'H', 'e', 'l', 'l', 'o'};
std::string str(vec.begin(), vec.end());  // 使用 vector 的迭代器构造字符串

// 2.数字数组
std::vector<int> vec = {1, 2, 3, 4, 5};
std::ostringstream oss;
    
for (size_t i = 0; i < vec.size(); ++i) {
   if (i != 0)
       oss << ",";  // 在每个元素之间加上逗号
   oss << vec[i];   // 将 int 转为字符串并添加到流中
}

std::string result = oss.str();  // 获取结果字符串

字符串截取

std::string str = "Hello, world!";
    
std::string substr2 = str.substr(7, 5);  // 从位置7开始截取5个字符
std::cout << substr2 << std::endl;  // 输出: "world"

判断包含关系

str.find(substr) != std::string::npos

字符串翻转

string reversed= string(words.rbegin(),words.rend())

字符串前补齐

// 补足前导0到32位
if (bin.length() < 32)
bin = std::string(32 - bin.length(), '0') + bin;

cout保留小数

#include <iomanip>

//四舍五入保留两位
cout<<setiosflags(ios::fixed)<<setprecision(2);
cout<<number;
//或者
printf("%.2f",number);

数字字符串互转

//数字转字符串
std::string str = std::to_string(number);
//字符串转数字
int intValue = std::stoi(intStr);
long longValue = std::stol(intStr);
double doubleValue = std::stod(floatStr);

查找子串

str.find("18") == string::npos

vector

增删

//清空
list.clear();
//在末尾添加
list.push_back(item);
//读取末尾、从末尾删除
item = list.back();
list.pop_back();

// 在某位置后插入
vec.insert(vec.begin() + index, element);

// 删除第一个某元素
auto it = std::find(vec.begin(), vec.end(), element);
if (it != vec.end()) {
    vec.erase(it);
}

// 删除所有某元素
vec.erase(std::remove(vec.begin(), vec.end(), element), vec.end());

遍历

for(auto iter : list) {
    //do something
}

// 获取某元素下标
auto it = std::find(vec.begin(), vec.end(), value_to_find);
if (it != vec.end()) {
    int index = it - vec.begin();
}

排序

#include <algorithm>

//简单类型升序
sort(v.begin(), v.end()); 
//简单类型降序
sort(v.begin(), v.end(), greater<int>()); 

//复杂类型
struct Interval { 
    int start, end; 
}; 
  
// Compares two intervals according to starting times. 
bool compareInterval(Interval i1, Interval i2) 
{ 
    return (i1.start < i2.start); //<表示升序,>表示降序
} 

sort(v.begin(), v.end(), compareInterval); 

unordered_map

  auto it = map.find(key);
    if (it != map.end()) {
        // 如果找到了键,则 it 是一个有效的迭代器
        std::cout << "Value for key " << key << " is " << it->second << std::endl;
    } else {
        // 如果未找到键,则 it 等于 map.end()
        std::cout << "Key " << key << " not found" << std::endl;
    }

//判断是否包含。
if(regionMap.count(key)) 包含;

//插入
map.emplace(key, value);
std::pair<int, string> element(1, "2");
map.insert(element);
map.insert(make_pair<int, string>(1, "2"));

//刪除
map.erase(key);

set

// vector转set
set<string> wordSet(words.begin(), words.end());
// set转vector 
std::vector<int> v(set.begin(), set.end());
//判断包含关系
set.find(element) != set.end();
//赋值
set.insert(element);
//删除
set.erase(element);

tuple

tuple<int, int> range = make_tuple(-1, -1);

//取值
cout << get<0>(range);
//赋值
get<0>(range) = 1;

MultiMap

#include <iostream>
#include <map>

std::multimap<int, std::string> mmap;
mmap.insert({1, "Apple"});
mmap.insert({2, "Banana"});
mmap.insert({1, "Apricot"});

// 查找所有键为 1 的元素
auto range = mmap.equal_range(1);
for (auto it = range.first; it != range.second; ++it) {
    std::cout << "Found: " << it->first << ": " << it->second << std::endl;
}

// 删除键为 2 的所有元素
mmap.erase(2);
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容