c++中stl的使用

本文通过实例演示了如何使用C++ STL中的容器如multiset和vector进行数据操作,包括元素的增删查改、排序、查找等,并展示了如何利用算法库进行高效的集合处理。

1 现有一整数集(允许有重复元素),初始为空(自定义赋值)。我们定义如下操作:
1-1
add x 把x加入集合
del x 把集合中所有与x相等的元素删除
ask x 对集合中元素x的情况询问
对每种操作,我们要求进行如下输出。
add 输出操作后集合中x的个数
del 输出操作前集合中x的个数
ask 先输出0表示x未被加入集合(0表示不曾加入),或者输出当前集合中x的个数。

1-2
对容器中的元素作以下操作(使用stl算法,自行构建恰当的数据,并附录运行结果):
删除小于6的元素
将所有5 改为 3
逆向
旋转
找出大于10的第一个元素
找出小于20的元素个数
遍历所有区间,每个元素+1
将该容器元素从降序排序
要求:Please use STL’s set or multiset or others to finish the task

2 理解并掌握stl模块

#include <iostream>
#include <set>
#include <string>
#include <vector>
#include <algorithm>
#include <functional>
#include <iomanip>

using namespace std;

//字符串分割函数  
std::vector<std::string> split(std::string str, std::string pattern)
{
    std::string::size_type pos;
    vector<std::string> result;
    str += pattern;//扩展字符串以方便操作  
    int size = str.size();

    for (int i = 0; i<size; i++)
    {
        pos = str.find(pattern, i);
        if (pos<size)
        {
            std::string s = str.substr(i, pos - i);
            result.push_back(s);
            i = pos + pattern.size() - 1;
        }
    }
    return result;
}
/*
1 现有一整数集(允许有重复元素),初始为空(自定义赋值)。我们定义如下操作:
1-1
add x 把x加入集合
del x 把集合中所有与x相等的元素删除
ask x 对集合中元素x的情况询问
对每种操作,我们要求进行如下输出。
add 输出操作后集合中x的个数
del 输出操作前集合中x的个数
ask 先输出0表示x未被加入集合(0表示不曾加入),或者输出当前集合中x的个数。
*/
void num1()
{
    multiset<string> mSet;
    set<string> onlySet;

    string commend;
    while (true)
    {
        getline(cin, commend);

        vector<string> v = split(commend, " ");
        string key = v[0];
        string value = v[1];
        if (key.compare("add") == 0)
        {
            mSet.insert(value);
            onlySet.insert(value);
        }
        else if (key.compare("del") == 0)
        {
            mSet.erase(value);
        }
        // 查询是否加入过
        if (onlySet.find(value) == onlySet.end())
        {
            cout << "0" << endl;
        }
        else
        {
            cout << value << " 的次数是:" << mSet.count(value) << endl;
        }

    }
}
/*
1-2
对容器中的元素作以下操作(使用stl算法,自行构建恰当的数据,并附录运行结果):
删除小于6的元素
将所有5 改为 3
逆向
旋转
找出大于10的第一个元素
找出小于20的元素个数
遍历所有区间,每个元素+1
将该容器元素从降序排序
要求:Please use STL’s set or multiset  or others to finish the task
*/
void num2()
{
    vector<int> v;
    for (size_t i = 1; i < 15; i++)
    {
        v.push_back(i);
    }

    ostream_iterator<int> output(cout, " "); // 可以这样写,666

    cout << setw(20) <<"原始数据:[" ;
    copy(v.begin(), v.end(), ostream_iterator<int> (cout, " ")); // 这岂不是没名字?
    cout << "]" <<endl;

    // 删除小于6的元素
    cout << setw(20) << "删除小于6的元素:[";
    v.erase(remove_if(v.begin(),v.end(),bind1st(greater_equal<int>(),6)),v.end()); // 最后一个参数不加删除不了
    copy(v.begin(), v.end(), output);
    cout << "]" << endl;

    // 将所有5 改为 3
    cout << setw(20) << "将所有5 改为 3:[";
    v.push_back(5);
    replace(v.begin(), v.end(), 5, 3);
    copy(v.begin(), v.end(), output);
    cout << "]" << endl;

    // 逆向
    cout << setw(20) << "逆向:[";
    reverse(v.begin(), v.end());
    copy(v.begin(), v.end(), output);
    cout << "]" << endl;

    // 旋转
    cout << setw(20) << "旋转:[";
    int mid = v.size() / 2;
    rotate(v.begin(), v.begin() + mid, v.end());
    copy(v.begin(), v.end(), output);
    cout << "]" << endl;

    // 找出大于10的第一个元素
    cout << setw(20) << "大于10的第一个元素:" << *find_if(v.begin(), v.end(), bind2nd(greater<int>(), 10)) << endl;

    // 找出小于20的元素个数
    cout << setw(20) << "小于20的元素个数:" << count_if(v.begin(), v.end(), bind2nd(less<int>(), 20)) << endl;

    // 遍历所有区间,每个元素 + 1
    vector<int>::iterator iter ;
    for (iter = v.begin(); iter != v.end(); ++iter)
    {
        *iter += 1;// 坑
        //cout << (*iter)+1 <<" ";
    }
    cout << setw(20) << "各元素加1后的数据:[";
    copy(v.begin(), v.end(), ostream_iterator<int>(cout, " ")); // 这岂不是没名字?
    cout << "]" << endl;
    // 将该容器元素从降序排序
    sort(v.begin(),v.end());
    cout << setw(20) << "降序的数据:[";
    copy(v.begin(), v.end(), ostream_iterator<int>(cout, " ")); // 这岂不是没名字?
    cout << "]" << endl;
}

void main()
{

    num2();

    system("pause");
}

遍历区间 每个元素-1

#include <stdlib.h>
#include <iostream>
#include <algorithm>
#include <iterator>
#include <algorithm>  
#include <functional>
#include <numeric>
#include <vector>
using namespace std;
class evenByTwo
{
private:
    int x;
public:
    evenByTwo() :x(0){}
    int operator()()
    {
        return   x += 2;
    }
};

int main()
{
    vector<int> ivector(4);
    ostream_iterator<int> output(cout, " ");
    //遍历区间 每个元素-1
    fill(ivector.begin(), ivector.end(),-1);
    copy(ivector.begin(), ivector.end(), output);
    cout << endl;
    generate(ivector.begin(), ivector.end(), evenByTwo());
    copy(ivector.begin(), ivector.end(), output);
    system("pause");
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值