C++研究

C++细节深度探索及软件工程

  C++博客 :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理 ::
  37 随笔 :: 0 文章 :: 74 评论 :: 0 Trackbacks

In STL , there are many method can traverse the vector object , but the most efficency one ,I think , it's 'for_each' function. And it can be used very near the real life language , what you need do is only to define a function express how you want to act  while it's traversing.

E.G

/********************************************************************
 created: 2007/04/19
 created: 19:4:2007   2:04
 filename:  C:\testvc6\TestStll\TestStll.cpp
 file path: C:\testvc6\TestStll
 file base: TestStll
 file ext: cpp
 author:  Chang xinglong(King.C)
 
 purpose: How to traverse a Vector ?
*********************************************************************/

 

You can define a travers Function which simply couput the node data , it was defined  like this :

void Traverse(int t)
{
    cout
<<t<<endl;
}
;

Then Create a integer vector , and use the for each to travers it ,here the code goes like this:
    vector <int> colt;
    
for (int i=0; i<=10;i++)
    
{
        colt.push_back(i);
    }

    for_each(colt.begin(),colt.end(),Traverse);
    cout
<<endl;


Very easy and effiency , try to use it in the program instead of the following code:
for (int i=0;i<max;i++)
{
   
int v =(int) aVector[i];
   traverse(v);
}
posted on 2007-04-19 02:18 常兴龙 阅读(695) 评论(0)  编辑 收藏 引用

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理


> hi的博客