STL:预备知识及简介

STL:预备知识及简介

参考文献《大道至简:C++ STL》

  • 一个demo
#include <iostream>
#include <cstring>
#include <list>

using namespace std;

struct per
{
    /* data */
    int     _no;
    double  _sc;

    void clear()
    {
        _no =   0;
        _sc =   0;   
    }
};

int main()
{
    per temp;
    temp.clear();

    list<per> p;
    p.clear();
    
    cout << "Press any to continue..." << endl;
    cin.get();

    int counter =   3;
    while(counter)
    {
        int no;
        double sc;

        cout << ">>" << endl;
        cin >> no >> sc;

        temp._no    =   no;
        temp._sc    =   sc;

        p.push_back(temp);

        memset(&temp, 0, sizeof(per));

        counter--;
    }

    for(list<per>::iterator pit = p.begin(); pit != p.end(); pit++)
    {
        temp.clear();
        temp = * pit;
        cout << temp._no << "," << temp._sc << endl;
    }

    return 0;
}
  • 文件读写demo
#include <iostream>
#include <fstream>

using namespace std;

int main()
{
    ifstream _ifs;
    ofstream _ofs;

    char file1[256];
    char file2[256];
    char context[256];

    printf("2 file name(in and out) >>");
    cin >> file1 >> file2;

    _ifs.open(file1, ios::in);
    _ofs.open(file2, ios::out);

    while (!_ifs.eof())
    {
        _ifs.getline(context, 128);
        _ofs << context << endl;
    }
    
    _ifs.close();
    _ofs.close();
    
    return 0;
}
  • 类模板的静态成员demo
#include <iostream>

using namespace std;

template <class Type> 
class BasicClass
{
public:
    static int _a;
    static float _b;
public:
    static int getA()
    {
        return _a;
    }
    static int getB()
    {
        return _b;
    }
};

template <class Type> int BasicClass<Type>::_a = 1;
template <class Type> float BasicClass<Type>::_b = 2.0;

int main()
{
    BasicClass<int> bc;
    bc._a = 2;
    bc._b = 3.2;
    cout << bc.getA() << "," << bc.getB() << endl;
    return 0;
}
  • for_each的用法demo
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>

using namespace std;

void print(string& s)
{
    cout << s << endl;
}


int main()
{
    vector<string> v_str(5);
    for (int i = 0; i < 5; i++)
    {
        getline(cin, v_str[i]);
    }
    for_each(v_str.begin(), v_str.end(), print);
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值