linux+stl软件,在Linux下使用STL快速入门

STL是Standard Template Library的简称,也即标准模板库,是一个具有工业强度的,高效的C++程序库。有关使用库的好处自不必多说,这里大致的讲述一下STL的相关知识和如何在Linux下使用STL;

STL是Standard Template Library的简称,也即标准模板库,是一个具有工业强度的,高效的C++程序库。有关使用库的好处自不必多说,这里大致的讲述一下STL的相关知识和如何在Linux下使用STL;

下面首先给出这几篇文章,都是介绍STL入门的极佳导学文章--

C++ STL轻松导学

STL简介

A modest STL tutorial

看了这篇文章之后,我想你一定对STL有了一个概括性质的了解,当然,如果你感觉自己了解的仍然不够充分,你可以去这里--

STL 中文站

最优秀的STL使用学习网站

C++ Standard Template Library Overview

获取更为详细的信息;其实网上有关STL的东西太多了,鱼龙混杂,但绝不乏精品,所以,如果你感觉看某一篇有关STL的文章实在是看不下去了,就应该考虑是不是这篇文章质量有问题,再去网上搜取合适的吧。

我想通览了以上有关STL的信息之后,我们可以得出这样一个结论,那就是在linux下,如果要使用STL库,可选的就是这两个:Boost和SGI;

下面我们分别来作一下比较:

这是C++ Boost 库的文档索引

这是C++ SGI 库的文档索引

经过比较,我们可以得出这样一个结论,Boost库的内容更为丰富一些;所以,如果你打算体验一下Linux下的基于STL的应用开发,可以准备从Boost的学习开始,而且Boost的网上文档也是非常丰富的,参与Boost的开发人员甚至达到了2000多人!

下面在Linux下编写一个基于STL的程序,测试一下;对了,现在我们也知道了Linux下默认支持的STL是SGI库,此库在Linux下性能非常不错!

这里先给出程序源代码:

#include

#include

using namespace std;

int main(int argc, char *argv[]){

vector int_vector;

vector double_vector;

int_vector.push_back(99);

int_vector.push_back(9999);

double_vector.push_back(99.99);

double_vector.push_back(9999.9999);

int size;

size = int_vector.size();

for (int i = 0; i < size; i ++){

cout << int_vector[i] << endl;

}

size = double_vector.size();

for (int j = 0; j < size; j ++){

cout << double_vector[j] << endl;

}

}

注意其中的这一句“using namespace std;”,如果没有这一句,你会遇到很多莫名其妙的问题,导致你不知所措,没办法排除错误,而实际上解决问题的关键就是这么一句话!

源码采用命令

g++ -o main main.cpp

即可编译成功;无需添加任何辅助的参数!

可能出的错误一:

In file included from /usr/lib/gcc/i386-neoshine-linux/3.4.4/../../../../include/c++/3.4.4/backward/new.h:33,

from /usr/include/g++-3/stl_algobase.h:52,

from /usr/include/g++-3/vector:30,

from main.cpp:2:

/usr/lib/gcc/i386-neoshine-linux/3.4.4/../../../../include/c++/3.4.4/backward/backward_warning.h:32:2: warning: #warning This file includes at least one deprecated or antiquated header. Please consider using one of the 32 headers found in section 17.4.1.2 of the C++ standard. Examples include substituting the header for the header for C++ includes, or instead of the deprecated header . To disable this warning use -Wno-deprecated.

In file included from /usr/lib/gcc/i386-neoshine-linux/3.4.4/../../../../include/c++/3.4.4/backward/new.h:34,

from /usr/include/g++-3/stl_algobase.h:52,

from /usr/include/g++-3/vector:30,

from main.cpp:2:

/usr/lib/gcc/i386-neoshine-linux/3.4.4/../../../../include/c++/3.4.4/new:82: error: declaration of `operator new' as non-function

/usr/lib/gcc/i386-neoshine-linux/3.4.4/../../../../include/c++/3.4.4/new:82: error: `size_t' is not a member of `std'

/usr/lib/gcc/i386-neoshine-linux/3.4.4/../../../../include/c++/3.4.4/new:82: error: expected `,' or `;' before "throw"

/usr/lib/gcc/i386-neoshine-linux/3.4.4/../../../../include/c++/3.4.4/new:83: error: declaration of `operator new []' as non-function

/usr/lib/gcc/i386-neoshine-linux/3.4.4/../../../../include/c++/3.4.4/new:83: error: `size_t' is not a member of `std'

/usr/lib/gcc/i386-neoshine-linux/3.4.4/../../../../include/c++/3.4.4/new:83: error: expected `,' or `;' before "throw"

/usr/lib/gcc/i386-neoshine-linux/3.4.4/../../../../include/c++/3.4.4/new:86: error: declaration of `operator new' as non-function

/usr/lib/gcc/i386-neoshine-linux/3.4.4/../../../../include/c++/3.4.4/new:86: error: `size_t' is not a member of `std'

/usr/lib/gcc/i386-neoshine-linux/3.4.4/../../../../include/c++/3.4.4/new:86: error: expected primary-expression before "const"

/usr/lib/gcc/i386-neoshine-linux/3.4.4/../../../../include/c++/3.4.4/new:86: error: expected `,' or `;' before "throw"

/usr/lib/gcc/i386-neoshine-linux/3.4.4/../../../../include/c++/3.4.4/new:87: error: declaration of `operator new []' as non-function

/usr/lib/gcc/i386-neoshine-linux/3.4.4/../../../../include/c++/3.4.4/new:87: error: `size_t' is not a member of `std'

这里之列举出来部分错误提示,这种错误看上去已经很恐怖了;解决方法就是前面提到的,千万不要忘了“using namespace std;”,因为,此时,所有的标准模板库均是在std域名空间内的,因此,必需要引用此空间名称才可以使用其成员。

如果想了解更多相关信息以及详细咨询,欢迎点击中英网http://www.uker.net/,或发email至:echo@uker.net,UKer.net资深编辑将为您详细解答。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值