50分 菜鸟::STL这东西怎么用???

lionnap 2004-12-10 12:05:43
事实我还没开始接触C++中面向对象的知识,各位能不能举些简单的例子用于理解?

比如说排个序呀什么什么的,大家能不能用完整的源代码说明一下。3kx!!!
...全文
203 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
jznsmail 2004-12-12
  • 打赏
  • 举报
回复
建議你之前可以看看C++TEMPLETE這書,之後如果你想深入研究STL那就看看STL源碼剖析吧!!!
oyljerry 2004-12-12
  • 打赏
  • 举报
回复
先学会用,然后研究,^_^
sharkhuang 2004-12-12
  • 打赏
  • 举报
回复
STL用好还是很难的
lionnap 2004-12-10
  • 打赏
  • 举报
回复
太复杂了 正如你说的 “还没学会走”

对1000个数进行排序,(任意方法,使用STL),并输出最大、最小、平均值

怎么写?
paybfly 2004-12-10
  • 打赏
  • 举报
回复
先学好C++的基础知识,摸板
bigmouse2002 2004-12-10
  • 打赏
  • 举报
回复
STL关键在于如何设计而不在于使用/
bigmouse2002 2004-12-10
  • 打赏
  • 举报
回复
/* The following code example is taken from the book
* "The C++ Standard Library - A Tutorial and Reference"
* by Nicolai M. Josuttis, Addison-Wesley, 1999
*
* (C) Copyright Nicolai M. Josuttis 1999.
* Permission to copy, use, modify, sell and distribute this software
* is granted provided this copyright notice appears in all copies.
* This software is provided "as is" without express or implied
* warranty, and with no claim as to its suitability for any purpose.
*/
#include <iostream>
#include <string>
#include <deque>
#include <set>
#include <algorithm>
using namespace std;


/* class Person
*/
class Person {
private:
string fn; // first name
string ln; // last name
public:
Person() {
}
Person(const string& f, const string& n)
: fn(f), ln(n) {
}
string firstname() const;
string lastname() const;
// ...
};

inline string Person::firstname() const {
return fn;
}

inline string Person::lastname() const {
return ln;
}

ostream& operator<< (ostream& s, const Person& p)
{
s << "[" << p.firstname() << " " << p.lastname() << "]";
return s;
}


/* binary function predicate:
* - returns whether a person is less than another person
*/
bool personSortCriterion (const Person& p1, const Person& p2)
{
/* a person is less than another person
* - if the last name is less
* - if the last name is equal and the first name is less
*/
return p1.lastname()<p2.lastname() ||
(p1.lastname()==p2.lastname() &&
p1.firstname()<p2.firstname());
}

int main()
{
// create some persons
Person p1("nicolai","josuttis");
Person p2("ulli","josuttis");
Person p3("anica","josuttis");
Person p4("lucas","josuttis");
Person p5("lucas","otto");
Person p6("lucas","arm");
Person p7("anica","holle");

// insert person into collection coll
deque<Person> coll;
coll.push_back(p1);
coll.push_back(p2);
coll.push_back(p3);
coll.push_back(p4);
coll.push_back(p5);
coll.push_back(p6);
coll.push_back(p7);

// print elements
cout << "deque before sort():" << endl;
deque<Person>::iterator pos;
for (pos = coll.begin(); pos != coll.end(); ++pos) {
cout << *pos << endl;
}

// sort elements
sort(coll.begin(),coll.end(), // range
personSortCriterion); // sort criterion

// print elements
cout << "deque after sort():" << endl;
for (pos = coll.begin(); pos != coll.end(); ++pos) {
cout << *pos << endl;
}
}

lionnap 2004-12-10
  • 打赏
  • 举报
回复
我就想看看是怎么用的嘛,散分还不行嘛~
bigmouse2002 2004-12-10
  • 打赏
  • 举报
回复
没学会走前,别学飞!stl要在你深刻领会类和模板之后再研究....
lionnap 2004-12-10
  • 打赏
  • 举报
回复
不仅仅限于排序哦,越多越好啦 呵呵 谢谢大家啦
goodluckyxl 2004-12-10
  • 打赏
  • 举报
回复
搂主的问题用STL实现和普通方法实现没什么区别
仅仅在于语法改一改
本来 排序
void sort( int*, int );
现在换成
template<typename T>
void sort( T*, int);
bigmouse2002 2004-12-10
  • 打赏
  • 举报
回复
那个叫函数模板,类模板才是精华!用模板实现一个vector类就知道了!
liweiswin 2004-12-10
  • 打赏
  • 举报
回复
#include<iostream>
#include<vector>
#include<algorithm>

using namespace std;

int main()
{
const int size=10; //数组长度
vector<int> num; //使用标准模板库里的容器模板定义一个INT的向量容器;
for(int i=0; i<size; i++)
num.push_back(size-i);
for(int i=0; i<size; i++)
{
cout<<num[i]<<" ";
if(i==10)
cout<<endl;
}

cout<<endl<<"排序后:"<<endl;

sort(num.begin(), num.end()); //使用泛型算法的升序排序函数

for(int i=0; i<size; i++)
{
cout<<num[i]<<" ";
if(i==10)
cout<<endl;
}

getchar();

return 0;
}

DEV C++下通过编译并运行成功。
其他的请楼主自己摸索。

64,637

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧