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;
}