【C/C++学院】(12)C++標準テンプレートライブラリSTL
5319 ワード
1.概要
STLのコードは広義にはalgorithm(アルゴリズム)、container(コンテナ)、iterator(反復器)の3つに分類される.
2.vectorベクトル
3.stack
4.queue
5.listチェーンテーブル入門-反復器使用方法の説明
6.stlアルゴリズムコールバック基礎
STLのコードは広義にはalgorithm(アルゴリズム)、container(コンテナ)、iterator(反復器)の3つに分類される.
2.vectorベクトル
#include "iostream"
#include "vector"
using namespace std;
//== != [] =
//(vector<int>
void printfArray(vector<int> &v)
{
int size = v.size();
for (int i = 0; i<v.size(); i++)
{
cout << v[i] << endl;
}
}
void main()
{
// ( )
vector<int> v1(5); //int v[5]
for (int i = 0; i<5; i++)
{
v1[i] = i + 1;
}
vector<int> v2(10);
v2 = v1;
for (int i = 0; i<5; i++)
{
cout << v2[i] << endl;
}
cout << v2.size() << endl;//5
v2.resize(0);
cout << v2.size() << endl;//0
vector<int> v3(3);
for (int i = 0; i<3; i++)
{
v3[i] = i + 10;//10, 11, 12
}
v3.push_back(3);
v3.push_back(4);
v3.push_back(5);
printfArray(v3);//10 11 12 3 4 5
system("pause");
}
#include "iostream"
#include "vector"
using namespace std;
struct Teacher
{
int age;
char name[10];
};
void main01()
{
struct Teacher t1, t2, t3;
t1.age = 11;
t2.age = 22;
t3.age = 33;
vector<Teacher> v3(0);
v3.push_back(t1);
v3.push_back(t2);
v3.push_back(t3);
for (int i = 0; i<3; i++)
{
cout << v3[i].age << endl;
}
system("pause");
}
void main()
{
struct Teacher t1, t2, t3;
t1.age = 11;
t2.age = 22;
t3.age = 33;
vector<Teacher *> v3(0);
v3.push_back(&t1);
v3.push_back(&t2);
v3.push_back(&t3);
for (int i = 0; i<3; i++)
{
//struct Teacher *tmp = (struct Teacher *)v3[i];
struct Teacher *tmp = v3[i];
cout << tmp->age << endl;
}
system("pause");
}
3.stack
#include "iostream"
#include "stack"
using namespace std;
void main()
{
//
stack<int> s;
//
for (int i = 0; i<5; i++)
{
//
s.push(i + 1);
}
//
while (!s.empty())
{
//
int tmp = s.top();
//
s.pop();
printf("%d
", tmp);
}
system("pause");
}
#include "iostream"
#include "stack"
using namespace std;
struct Teacher
{
int age;
char name[10];
};
void printfStack(stack<Teacher> &s)
{
//
while (!s.empty())
{
//
cout << s.top().age << endl;
//
s.pop();
}
}
void main()
{
//
Teacher t1, t2, t3;
t1.age = 11;
t2.age = 22;
t3.age = 33;
stack<Teacher> s;
s.push(t1);
s.push(t2);
s.push(t3);
printfStack(s);
system("pause");
}
4.queue
#include "iostream"
#include "queue"
using namespace std;
void main()
{
//
queue<int> q;
//x
for (int i = 0; i<5; i++)
{
q.push(i);
}
while (!q.empty())
{
//
int tmp = q.front();
cout << tmp << endl;
//
q.pop();
}
system("pause");
}
#include "iostream"
#include "queue"
using namespace std;
struct Teacher
{
int age;
char name[10];
};
void printfFont(queue<Teacher *> &q)
{
while (!q.empty())
{
//
Teacher * tmp = q.front();
cout << tmp->age << endl;
//
q.pop();
}
}
void main()
{
//
queue<Teacher *> q;
Teacher t1, t2, t3;
t1.age = 11;
t2.age = 22;
t3.age = 33;
//x
q.push(&t1);
q.push(&t2);
q.push(&t3);
printfFont(q);
// while(!q.empty())
// {
// //
// Teacher * tmp = q.front();
// cout<<tmp->age<<endl;
// //
// q.pop();
// }
system("pause");
}
5.listチェーンテーブル入門-反復器使用方法の説明
#include "iostream"
#include "list"
using namespace std;
void main()
{
//
list<int> l;
int len = l.size();
cout << len << endl;
// ( )
for (int i = 0; i<5; i++)
{
l.push_back(i);
}
len = l.size();
cout << len << endl;
// iterator
list<int>::iterator current = l.begin();
//l.end();
while (current != l.end())
{
cout << *current << endl;
current++;
}
cout << "********** ************" << endl;
//
current = l.begin();
current++;
current++;
current++;
l.insert(current, 100);
for (list<int>::iterator p = l.begin(); p != l.end(); p++)
{
cout << (*p) << endl;
}
system("pause");
}
6.stlアルゴリズムコールバック基礎
#include "iostream"
#include "algorithm"
#include "cmath"
#include "vector"
using namespace std;
// , ,stl ,
void callbakFunc(int &v)
{
cout << v << endl;
}
int comp(const int &a, const int &b)
{
return a<b;
}
void main()
{
vector<int> v(5);
for (int i = 0; i<5; i++)
{
v[i] = rand() % 10;
}
for (int i = 0; i<5; i++)
{
printf("%d ", v[i]);
}
for_each(v.begin(), v.end(), callbakFunc);
sort(v.begin(), v.end(), comp);
for (int i = 0; i<5; i++)
{
printf("%d ", v[i]);
}
system("pause");
}