栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

STL:预备知识及简介

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

STL:预备知识及简介

STL:预备知识及简介

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

  • 一个demo
#include 
#include 
#include 

using namespace std;

struct per
{
    
    int     _no;
    double  _sc;

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

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

    list 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::iterator pit = p.begin(); pit != p.end(); pit++)
    {
        temp.clear();
        temp = * pit;
        cout << temp._no << "," << temp._sc << endl;
    }

    return 0;
}
  • 文件读写demo
#include 
#include 

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 

using namespace std;

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

template  int BasicClass::_a = 1;
template  float BasicClass::_b = 2.0;

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

using namespace std;

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


int main()
{
    vector 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;
}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/347960.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号