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...