C++ OJ常用語法

VSCODE code runner設(shè)置

executerMap中:
windows

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

linux

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

字符串

cin輸入字符串

string a;
getline(cin,a);

// 讀取某行數(shù)字
int number;
cin >> number;
cin.ignore(); // 清掉換行符,讀取換行

字符串分割

#include<sstream>
#include<string>
//1、stringstream 或者istringstream
vector<string> substrings;
istringstream input(str);   //讀取str到字符串流中
string temp;
char pattern = ','; 
//使用getline函數(shù)從字符串流中讀取,遇到分隔符時(shí)停止,和從cin中讀取類似
//注意,將空格作為分割符時(shí),getline會(huì)把空格轉(zhuǎn)換為空串,建議使用方法2
while(getline(input, temp, pattern)){
    substrings.push_back(temp);
}
//2、istringstream,分割空格
string word;
istringstream ss(str);
while (ss >> word) {
    //do something
}

//3.字符串轉(zhuǎn)vector<char>

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

for (char c : str) {
    charVector.push_back(c);
}
// vector轉(zhuǎn)字符串

// 1. char數(shù)組
std::vector<char> vec = {'H', 'e', 'l', 'l', 'o'};
std::string str(vec.begin(), vec.end());  // 使用 vector 的迭代器構(gòu)造字符串

// 2.數(shù)字?jǐn)?shù)組
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 << ",";  // 在每個(gè)元素之間加上逗號(hào)
   oss << vec[i];   // 將 int 轉(zhuǎn)為字符串并添加到流中
}

std::string result = oss.str();  // 獲取結(jié)果字符串

字符串截取

std::string str = "Hello, world!";
    
std::string substr2 = str.substr(7, 5);  // 從位置7開始截取5個(gè)字符
std::cout << substr2 << std::endl;  // 輸出: "world"

判斷包含關(guān)系

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

字符串翻轉(zhuǎn)

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

字符串前補(bǔ)齊

// 補(bǔ)足前導(dǎo)0到32位
if (bin.length() < 32)
bin = std::string(32 - bin.length(), '0') + bin;

cout保留小數(shù)

#include <iomanip>

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

數(shù)字字符串互轉(zhuǎn)

//數(shù)字轉(zhuǎn)字符串
std::string str = std::to_string(number);
//字符串轉(zhuǎn)數(shù)字
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);

// 刪除第一個(gè)某元素
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
}

// 獲取某元素下標(biāo)
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>()); 

//復(fù)雜類型
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 是一個(gè)有效的迭代器
        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轉(zhuǎn)set
set<string> wordSet(words.begin(), words.end());
// set轉(zhuǎn)vector 
std::vector<int> v(set.begin(), set.end());
//判斷包含關(guān)系
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);
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容