c++常见Built-in总结

本文介绍了C++中字符串的基本操作,包括判断字符类型、获取字符串长度及空字符串的检查方法。此外,还详细讲解了如何对C++标准库中的容器如map、vector、stack、queue进行初始化、插入和删除等基本操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值