c++常見Built-in總結

1、判斷是否爲數字或字母

isalpha:如果是字母,返回一個非零數;否則,返回0;

isdigit:如果是數字(0-9),返回一個非零數;否則,返回0;

isalnum:如果是字母或數字,返回一個非零數;否則,返回0;


2、獲得長度

vector的長度:vector<int> a; return a.size();

string的長度:string s; return s.length() / return s.size();


3、字符串

(1)字符串長度

c++中字符串以'/0'結尾,通過s.size()或s.length()獲得的長度爲字符串中包含的字符的個數,不包含'/0'

(2)空字符串

空字符串指的是不包含任何字符的字符串,含有空格的字符串不是空字符串

#include<iostream>
#include<string>
using namespace std;

int main(){
    string a = "hello";
    string b = " ";
    string c = "";
    if(a[6] == '\0') cout << "yes" << endl; else cout << "false" << endl;
    if(b[0] == '\0') cout << "yes" << endl; else cout << "false" << endl;
    if(c[0] == '\0') cout << "yes" << endl; else cout << "false" << endl;
    cout <<  "a's length:" << a.size() << endl;
    cout <<  "b's length:" << b.size() << endl;
    cout <<  "c's length:" << c.size() << endl;
    cout <<  "b is empty? " << b.empty() << endl;
    cout <<  "c is empty? " << c.empty() << endl;
}

運行結果:

yes

false

yes

a's length:5

b's length:1

c's length:0

b is empty? 0

c is empty? 1


4、容器的初始化、插入、刪除等操作

map: map<int,int> m; m[1]=1;

vector: vector<int> v; v.push_back(1);

stack: stack<int> s; s.push(1); s.pop(); int top=s.top();

queue: quene<int> q; q.push(1); q.pop(); int top=q.front();


5、使用iterator對容器進行遍歷

vector<int>:: iterator iter1;

for(iter1=pushV.begin(); iter1!=pushV.end(); iter1.++) m_data.push(*iter1);

更新ing...

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章