C++編程常用函數(shù)

1.大小寫轉(zhuǎn)換;
  if(islower(str[i]))
      str[i] = toupper(str[i]);
  else
      str[i] = tolower(str[i]);

2.unique()是C++標(biāo)準(zhǔn)庫函數(shù)里面的函數(shù),其功能是去除相鄰的重復(fù)元素(只保留一個),所以使用前需要對數(shù)組進(jìn)行排序
        sort(s1.begin(), s1.end());
        auto it = unique(s1.begin(), s1.end());
        s1.erase(it, s1.end());

3.str.find() == -1;沒找到, str.npos = -1;
  if (str.find("abc") == string::npos) { ... }

  錯誤:if(str.find("abc") ) 
  注:找不到abc會返回-1,不為0為True。0為False 

4.priority_queue<int> xxx 大根堆

  priority_queue<int, vector<int>, greater<int>> xxxx 小根堆

5.vector<int> a;
 a.erase(a.begin() + i);

6.map<int, int> m;
 m.erase(key);

7.memset(h, 0x3f, sizeof h); 按字節(jié)賦值,一個字節(jié)8位。

8.
bool cmp(pair<int ,int > a, pair<int, int > b)
{
    return a.second < b.second;
}
//然后使用sort()函數(shù)
sort(data.begin(), data.end(), cmp);

struct Edge
{
    int from, to, weight;
};
bool cmp(Edge a, Edge b)
{
    return a.weight > b.weight;
}
 
//然后使用sort()函數(shù)
sort(data.begin(), data.end(), cmp);

9.字符串轉(zhuǎn)整型。
string str = "123";
int a = stoi(str); //c++
int a = atoi(str.c_str());//c

10.int isdigit(char c)  // 檢查c是否是數(shù)字字符。
string str = "123rt"
if (isdigit(str[i])) // 如果是返回1, 否則返回0;
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

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

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