C++函数

经过一段时间时间的学习,意识到了C++的博大精深,建议各位系统的学习C++,不然只是囫囵吞枣,不要只会它里面的部分函数!!!!!
1.Stack
top()返回栈顶元素,并不移除这个元素
empty()如果栈空返回true,否则false
size()栈的大小
void push()插入元素到栈顶
void pop()移除栈顶元素

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

void main()
{
    stack<char> v;
    for(int i=0; i<10; i++)
        v.push(i+97);

    cout<<v.size()<<endl;

    while(!v.empty())
    {
        cout<<v.top()<<" ";
        v.pop();
    }
}

2.queue
empty()判空
front()返回队头元素
pop()删除对头元素
back()返回队尾元素
push()在队尾加入元素
size()大小


#include<iostream>
#include<queue>
using namespace std;
int main()
{
    queue<int>q;
    for(int i=0; i<5; i++)
        q.push(i);
    while(!q.empty()) //or while(q.size())
    {
        cout<<q.front();
        q.pop();
    }
    return 0;
}
//树的非递归遍历,层次遍历什么的,不用自己再写栈和队列了,直接利用STL中的就行了
快排:
头文件:
#include <algorithm>
using namespace std;

bool compare(int a,int b)
{
    return a<b; //升序排列,如果改为return a>b,则为降序
}

1.默认的sort函数是按升序排。对应于1)
sort(a,a+n);   //两个参数分别为待排序数组的首地址和尾地址
2.可以自己写一个cmp函数,按特定意图进行排序。对应于2)
例如:
(1)对数组a降序排序:
int cmp( const int &a, const int &b )
{
    if( a > b )
        return 1;
    else
        return 0;
}
sort(a,a+n,cmp);
(2)先按x升序排序,若x值相等则按y升序排:
int cmp( const POINT &a, const POINT &b )
{
    if( a.x < b.x )
        return 1;
    else if( a.x == b.x )
    {
        if( a.y < b.y )
            return 1;
        else
            return 0;
    }
    else
        return 0;
}
sort(a,a+n,cmp);

 

3、反转字符串:
#include<bits/stdc++.h>
#include<string>
using namespace std;
int main()
{
	char a[1005];
	int t;
	cin>>t;
	getchar(); //读入换行 
	while(t--)
	{
	gets(a);
    int len=strlen(a);
    reverse(a,a+len);
    cout<<a<<endl;
	}
   	return 0;
 }
4、lower_bound的作用是查找“大于或者等于x的第一个位置”。
5、unique函数可以删除有序数组中的重复元素。
6、不定长数组:vector
    vector就是一个不定长数组。
    它把一些常用操作“封装”在了vector类型内部。
    基本操作
    (1)头文件#include<vector>;
    (2)创建vector对象,vector<int> vec;
    (3)尾部插入数字:vec.push_back(a);
    (4)使用下标访问元素,cout<<vec[0]<<endl;记住下标是从0开始的。
    (5)使用迭代器访问元素.
    <span style="font-size:18px;">vector<int>::iterator it;
    for(it=vec.begin();it!=vec.end();it++)
        cout<<*it<<endl;</span>
    (6)插入元素:    vec.insert(vec.begin()+i,a);在第i个元素后面插入a;
    (7)删除元素:    vec.erase(vec.begin()+2);删除第3个元素
        vec.erase(vec.begin()+i,vec.end()+j);删除区间[i,j-1];区间从0开始
    (8)向量大小:vec.size();
       向尾部添加元素:vec.push_back();
       删除最后一个元素:vec.pop_back);
    (9)清空:vec.clear()   //清空之后,vec.size()为0
    vector<Edge>G[maxn]; 
    void init()
    {
        for(int i=0;i<=n;i++)
            G[i].clear();
        memset(vis,0,sizeof(vis));
    }
7,map的基本操作函数:
#include <stdio.h>

 C++ Maps是一种关联式容器,包含“关键字/值”对
begin()             返回指向map头部的迭代器 
clear()            删除所有元素 
count()            返回指定元素出现的次数
empty()           如果map为空则返回true 
end()               返回指向map末尾的迭代器 
equal_range()   返回特殊条目的迭代器对
erase()             删除一个元素
find()               查找一个元素
get_allocator()  返回map的配置器
insert()            插入元素
key_comp()      返回比较元素
key的函数 
lower_bound()   返回键值>=给定元素的第一个位置
max_size()       返回可以容纳的最大元素个数
rbegin()            返回一个指向map尾部的逆向迭代器
rend()              返回一个指向map头部的逆向迭代器
size()               返回map中元素的个数
swap()             交换两个map 
upper_bound() 返回键值>给定元素的第一个位置
value_comp()   返回比较元素value的函数
迭代器就是指针;
指向这个关联容器啊,关联容器有键和值,first就是键,second就是值。
#include <iostream>//输入多组字符串,统计每个字符串出现的次数。
#include <map>
using namespace std;
int main()
{
   int n;
   string st;
   map <string, int> mp;
   while(cin>>n)
   {
       mp.clear();
       for(int i=0; i<n; i++)
       {
            cin >> st;
            mp[st]++;
       }
       map<string, int>::iterator it;
       for(it = mp.begin(); it!=mp.end(); it++)
       {
            cout << "[" << it->first << "] = " << it->second << "\n";
       }
   }
    return 0;
}

8.字符串:

操作string字符数组
定义字符串string s;char s[100];
取得第i个字符s[i]s[i]
字符串长度s.length()
或 s.size()
strlen(s)
读入一行getline(cin, s);gets(s);
赋值s = "you";strcpy(s, "you");
字符串连接s = s + "you";
s += "you";
strcat(s, "you");
字符串比较s == "you"strcmp(s, "you");
#include<iostream>
#include"string"
#include"algorithm"
using namespace std;

//string的赋值
void f1()
{
    string s1 ="shihao";        //string是一个类
    string s2 ("bbbbbb");
    string s3 = s1;
    string s4(10, 'a');     //等价于string s4 ="aaaaaaaaaa";
    cout << s1 << endl;
    cout << s2 << endl;
    cout << s3 << endl;
    cout << s4 << endl;
}

//string的遍历
void f2()
{
    string s1 = "abcdefg";

    //1:数组方式
    for(int i = 0;  i < s1.length(); i++)
    {
        cout << s1[i] << " ";
    }
    cout << endl;

    //2:迭代器
    for(string::iterator it = s1.begin(); it != s1.end(); it++)
    {
        cout << *it << " ";
    }
    cout << endl;

    //3  异常处理
    try
    {
        for(int i = 0; i < s1.length() + 3; i++)
        {
            cout << s1.at(i) << " ";   //当出现错误时, 会向外抛出异常
        }
    }
    catch(...)
    {
        cout << "发生异常" << endl;
    }

    try
    {
        for(int i = 0;  i < s1.length() + 3; i++)   //出现错误, 不向外抛出异常
        {
            cout << s1[i] << " ";
        }
    }
    catch(...)
    {
        cout << "发生异常" << endl;
    }
}

//字符指针和sting的转换
void f4()
{
    //1:
    string s1 = "aaabbb";
    cout << s1.c_str() << endl;

    //2:
    char buf[128] = {0};
    s1.copy(buf, 3, 1);    //拷贝3个字符,从1个字符开始(位置下标从0开始)    //注:不会自动加上字符串结束标志
    cout << "buf: " << buf << endl;
}

//字符串的连接
void f5()
{
    //1:
    string s1 = "aaa";
    string s2 = "bbb";
    s1 = s1 + s2;
    cout << "s1: " << s1 << endl;

    //2:
    string s3 = "333";
    string s4 = "444";
    s3.append(s4);
    cout << "s3: " << s3 << endl;
}

//字符串的查找和替换
void f6()
{
    string s1 = "wbm hello wbm 111 wbm 222 wbm 333";

    //查找 从查找位置开始第一个出现的下标
    int index = s1.find("wbm", 1);  //位置下标从0开始
    cout <<"index: " << index << endl << endl;

    //查找每一次wbm出现的下标
    int offindex = s1.find("wbm", 0);
    while (offindex != string::npos)   //不等于-1
    {
        cout << "offindex: " << offindex << endl;
        offindex++;
        offindex = s1.find("wbm", offindex);
    }

    //把所有的wbm换成大写
    offindex = s1.find("wbm", 0);
    while (offindex != string::npos)   //不等于-1
    {
        s1.replace(offindex, 3, "WBM");
        offindex++;
        offindex = s1.find("wbm", offindex);
    }
    cout << endl << "s1替换后的结果为: " << s1 << endl;

    //把aaa替换成大写
    string s2 = "aaa bbb ccc";
    s2.replace(0, 3, "AAA");    //从第0个位置开始替换3个
    cout << endl << "s2: " << s2 << endl;
}

//区间删除和插入
void f7()
{
    string s1 = "hello1 hello2 hello1";
    string::iterator it = find(s1.begin(), s1.end(), 'l');
    if (it != s1.end())
    {
        s1.erase(it);   //删除
    }
    cout << "s1删除l以后的结果为:" << s1 << endl;

    s1.erase(s1.begin(), s1.end());
    cout << "s1全部删除:" << s1 << endl;
    cout << "s1的长度为: " << s1.length() << endl;

    //插入
    string s2 = "BBB";
    s2.insert(0, "AAA");
    cout << "s2: " << s2 << endl;
}

//大小写转换
void f8()
{
    string s1 = "AAAbbb";
    transform(s1.begin(), s1.end(), s1.begin(), ::tolower);       //transform(first,last,result,op);
    cout << "s1全部转化为小写为: " << s1 << endl;
    transform(s1.begin(), s1.end(), s1.begin(), ::toupper);       //transform(first,last,result,op);
    cout << "s1全部转化为大写为: " << s1 << endl;
}

int main()
{
    f8();
    return 0;
}

 

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值