用list的成员函数插入元素到list中
[ 2005-06-23 23:17:14 | 作者: Admin ]
list的成员函数push_front()和push_back()分别把元素加入到list的前面和后面。你可以使用insert() 把对象插入到list中的任何地方。
insert()可以加入一个对象,一个对象的若干份拷贝,或者一个范围以内的对象。这里是一些 插入对象到list中的例子:
阅读全文...
insert()可以加入一个对象,一个对象的若干份拷贝,或者一个范围以内的对象。这里是一些 插入对象到list中的例子:
/*
|| Using insert to insert elements into a list.
*/
#include <list>
int main (void) {
list<int> list1;
/*
|| Put integers 0 to 9 in the list
*/
for (int i = 0; i < 10; ++i) list1.push_back(i);
...|| Using insert to insert elements into a list.
*/
#include <list>
int main (void) {
list<int> list1;
/*
|| Put integers 0 to 9 in the list
*/
for (int i = 0; i < 10; ++i) list1.push_back(i);
阅读全文...
使用list的成员函数sort()排序一个list
[ 2005-06-23 22:53:08 | 作者: Admin ]
要排序一个list,我们要用list的成员函数sort(),而不是通用算法sort()。所有我们用过的算法都是 通用算法。然而,在STL中有时容器支持它自己对一个特殊算法的实现,这通常是为了提高性能。
在这个例子中,list容器有它自己的sort算法,这是因为通用算法仅能为那些提供随机存取里面元素 的容器排序,而由于list是作为一个连接的链表实现的,它不支持对它里面的元素随机存取。所以就需要一个特殊的 sort()成员函数来排序list。
由于各种原因,容器在性能需要较高或有特殊效果需求的场合支持外部函数(extra functions), 这通过利用构造函数的结构特性可以作到。
for_each 算法声明
阅读全文...
在这个例子中,list容器有它自己的sort算法,这是因为通用算法仅能为那些提供随机存取里面元素 的容器排序,而由于list是作为一个连接的链表实现的,它不支持对它里面的元素随机存取。所以就需要一个特殊的 sort()成员函数来排序list。
由于各种原因,容器在性能需要较高或有特殊效果需求的场合支持外部函数(extra functions), 这通过利用构造函数的结构特性可以作到。
for_each 算法声明
template<class InIt, class Fun>
Fun for_each(InIt first, InIt last, Fun f);
...Fun for_each(InIt first, InIt last, Fun f);
阅读全文...
使用STL通用算法search在list中找一个序列
[ 2005-06-23 22:12:14 | 作者: Admin ]
作者:Scott Field
一些字符在STL容器中很好处理,让我们看一看一个难处理的字符序列。我们将定义一个list来放字符。
list<char> Characters;
现在我们有了一个字符序列,它不用任何帮助就知道然后管理内存。它知道它是从哪里开始、到哪里结束。 它非常有用。我不知道我是否说过以null结尾的字符数组。
让我们加入一些我们喜欢的字符到这个list中:
我们将得到多少个空字符呢?
阅读全文...
一些字符在STL容器中很好处理,让我们看一看一个难处理的字符序列。我们将定义一个list来放字符。
list<char> Characters;
现在我们有了一个字符序列,它不用任何帮助就知道然后管理内存。它知道它是从哪里开始、到哪里结束。 它非常有用。我不知道我是否说过以null结尾的字符数组。
让我们加入一些我们喜欢的字符到这个list中:
Characters.push_back('\0');
Characters.push_back('\0');
Characters.push_back('1');
Characters.push_back('2');
Characters.push_back('\0');
Characters.push_back('1');
Characters.push_back('2');
我们将得到多少个空字符呢?
int NumberOfNullCharacters(0);
count(Characters.begin(),
...count(Characters.begin(),
阅读全文...
使用STL通用算法find_if()在list中搜索对象
[ 2005-06-23 21:50:14 | 作者: Admin ]
这是find()的一个更强大的版本。这个例子演示了find_if(),它接收一个函数对象的参数作为参数, 并
使用它来做更复杂的评价对象是否和给出的查找条件相付。
假设我们的list中有一些按年代排列的包含了事件和日期的记录。我们希望找出发生在1997年的事件。
阅读全文...
使用它来做更复杂的评价对象是否和给出的查找条件相付。
假设我们的list中有一些按年代排列的包含了事件和日期的记录。我们希望找出发生在1997年的事件。
/*
|| How to find things in an STL list MkII
*/
#include <string>
#include <list>
#include <algorithm>
class EventIsIn1997 {
public:
bool operator () (string& EventRecord) {
// year field is at position 12 for 4 characters in EventRecord
...|| How to find things in an STL list MkII
*/
#include <string>
#include <list>
#include <algorithm>
class EventIsIn1997 {
public:
bool operator () (string& EventRecord) {
// year field is at position 12 for 4 characters in EventRecord
阅读全文...
STL 中find()在list中查找对象
[ 2005-06-23 21:34:20 | 作者: Admin ]
声明如下:
template<class InIt, class T>
InIt find(InIt first, InIt last, const T& val);
first 首指针
last 尾指针
val 查找内容
代码举例:
阅读全文...
template<class InIt, class T>
InIt find(InIt first, InIt last, const T& val);
first 首指针
last 尾指针
val 查找内容
代码举例:
/*
|| How to find things in an STL list
*/
#include <string>
#include <list>
#include <algorithm>
int main (void) {
list<string> Fruit;
list<string>::iterator FruitIterator;
Fruit.push_back("Apple");
Fruit.push_back("Pineapple");
...|| How to find things in an STL list
*/
#include <string>
#include <list>
#include <algorithm>
int main (void) {
list<string> Fruit;
list<string>::iterator FruitIterator;
Fruit.push_back("Apple");
Fruit.push_back("Pineapple");
阅读全文...
Nate Robin 的OGL教学软件.
[ 2005-06-23 15:58:56 | 作者: Admin ]
你没有阅读此日志的权限.
STL:sort()用法举例
[ 2005-06-23 13:04:08 | 作者: Admin ]
STL:sort()用法举例
阅读全文...
// test program for sort algorithm
#include <stdlib.h>
#include <iostream.h>
#include <algo.h>
#include <vector.h>
class INT {
public:
INT() {}
INT(int a) { d=a; }
INT(const INT& a) { d=a.d; }
INT& operator=(const INT& a) { d=a.d;return *this; }
int operator<(const INT& a) { return d<a.d; }
operator int() const { return d; }
...#include <stdlib.h>
#include <iostream.h>
#include <algo.h>
#include <vector.h>
class INT {
public:
INT() {}
INT(int a) { d=a; }
INT(const INT& a) { d=a.d; }
INT& operator=(const INT& a) { d=a.d;return *this; }
int operator<(const INT& a) { return d<a.d; }
operator int() const { return d; }
阅读全文...
// test program for iterator
#include <iostream.h>
#include <deque.h>
void main() {
deque<int> q;
q.push_back(1);
q.push_back(2);
q.push_back(3);
q.push_back(4);
for(deque<int>::iterator i=q.begin();
i != q.end(); i++)
cout<<*i<<endl;
}
#include <iostream.h>
#include <deque.h>
void main() {
deque<int> q;
q.push_back(1);
q.push_back(2);
q.push_back(3);
q.push_back(4);
for(deque<int>::iterator i=q.begin();
i != q.end(); i++)
cout<<*i<<endl;
}
deque<int>容器在定义时给定其储存int型别的物件,存入一些int物件后,我们想要浏览之。宣告deque<int>::iterator ...
阅读全文...
VECTOR使用举例
[ 2005-06-23 12:23:28 | 作者: Admin ]
// test program for vector container
#include <iostream.h>
#include <vector.h>
void main() {
vector<char*> v;
v.push_back("me");
v.push_back("you");
v.push_back("he");
v.push_back("she");
for(int i=0;i<4;i++)
cout<<v[i]<<" ";
}
//end程式输出
me
you
he
she
#include <iostream.h>
#include <vector.h>
void main() {
vector<char*> v;
v.push_back("me");
v.push_back("you");
v.push_back("he");
v.push_back("she");
for(int i=0;i<4;i++)
cout<<v[i]<<" ";
}
//end程式输出
me
you
he
she
vector<data_type>表示我们可以放入各种资料型别到<...>之中,ve...
阅读全文...










