注册 登录
编程论坛 C++教室

请教 一段 C++ STL 程序运行速度慢的问题

Rebecca0397 发布于 2010-04-01 13:33, 877 次点击
下面的函数运行速度非常慢,哪些地方有问题呢?谢谢!

// Find the address of the company with the given name.
string FindAddress( list<Company> companies, string name )
{
  for( list<Company>::iterator i = companies.begin(); i != companies.end(); i++ )
  {
    if( *i == name )
    {
      return (*i).address;
    }
  }
  return "";
}

3 回复
#2
秀痘魔导士2010-04-01 13:36
查找用map
#include <map>

还有*i应该是company对象,和string去匹配?company应该是结构体对象吧,(*i).name?
#3
Rebecca03972010-04-01 13:42
回复 2楼 秀痘魔导士
谢谢,我是个STL菜鸟,你的意思是应该把list 换成 map??
#4
秀痘魔导士2010-04-01 13:46
如果数据量大的话,用map效率高点,o(1)。
 if( *i == name )
这句看下是不是有错。
1