STL快速入门

来源: 作者:articleman
  

在Linux下使用STL快速入门

引自:http://linux.chinaitlab.com/c/717290.html 

 

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

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

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

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

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

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

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

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

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

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

这里先给出程序源代码:

#include <iostream>
#include <vector>
using namespace std;
int main(int argc, char *argv[]){
      vector<int> int_vector;
      vector<double> 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 <X> header for the <X.h> header for C++ includes, or <iostream> instead of the deprecated header <iostream.h>. 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域名空间内的,因此,必需要引用此空间名称才可以使用其成员。

 

 


时间:2007-04-12 00:08 来源: 作者:articleman 原文链接

好文,顶一下
(0)
0%
文章真差,踩一下
(0)
0%
------分隔线----------------------------


把开源带在你的身边-精美linux小纪念品
无觅相关文章插件,快速提升流量