相关疑难解决方法(0)

如何从std :: map中检索所有键(或值)并将它们放入向量中?

这是我出来的可能方式之一:

struct RetrieveKey
{
    template <typename T>
    typename T::first_type operator()(T keyValuePair) const
    {
        return keyValuePair.first;
    }
};

map<int, int> m;
vector<int> keys;

// Retrieve all keys
transform(m.begin(), m.end(), back_inserter(keys), RetrieveKey());

// Dump all keys
copy(keys.begin(), keys.end(), ostream_iterator<int>(cout, "\n"));
Run Code Online (Sandbox Code Playgroud)

当然,我们也可以通过定义另一个仿函数RetrieveValues从地图中检索所有值.

有没有其他方法可以轻松实现这一目标?(我总是想知道为什么std :: map不包含我们这样做的成员函数.)

c++ dictionary stl stdmap

222
推荐指数
16
解决办法
30万
查看次数

与VC++ 6兼容的Boost的最新版本是什么?

什么是与Microsoft Visual C++ 6兼容的Boost库的最新版本?你能提供直接下载的链接吗?

http://www.boost.org上的 "下载"链接仅提供版本1.36.0的下载,该版本的文档列出了Visual C++ 7.1作为Microsoft编译器测试的最低版本.他们是否清除旧版本的下载?

c++ boost visual-c++-6 visual-c++

14
推荐指数
3
解决办法
4075
查看次数

创建STL映射键迭代器

通常,您有一个映射map<string,X>,其中键是映射值的名称,您需要一个API,让消费者可以看到所有名称...例如填充GUI列表框.您可以构建一个向量并将其作为API调用返回,但这样效率很低.您可以只返回对地图的引用,但随后也可以访问这些值,您可能不希望这样.

那么你怎么能编写一个兼容的类KeyIterator,它包装map并提供对该map中键的标准迭代器访问.

例如:

map<string,X> m= ...
KeyIterator<string> ki(m);
for(KeyIterator<string>::iterator it=ki.begin();it!=ki.end();++it)
 cout << *it;
Run Code Online (Sandbox Code Playgroud)

KeyIterator应该是轻量级的,因此您可以从几乎没有开销的方法返回它.

编辑: 我不确定我是否完美解释,让我给出一个更好的用例(半伪):

class PersonManager
{
 private:
  map<string,Person> people;
 public:
  //this version has to iterate the map, build a new structure and return a copy
  vector<string> getNamesStandard();

  //this version returns a lightweight container which can be iterated
  //and directly wraps the map, allowing access to the keys
  KeyIterator<string> getNames();
};

void PrintNames(PersonManager &pm)
{
 KeyIterator<string> names = pm.getNames();
 for(KeyIterator<string>::iterator it=names.begin();it!=names.end();++it)
  cout << *it << endl; …
Run Code Online (Sandbox Code Playgroud)

c++ stl

7
推荐指数
1
解决办法
3068
查看次数

标签 统计

c++ ×3

stl ×2

boost ×1

dictionary ×1

stdmap ×1

visual-c++ ×1

visual-c++-6 ×1