• <small id='4kM5K'></small><noframes id='4kM5K'>

        <legend id='4kM5K'><style id='4kM5K'><dir id='4kM5K'><q id='4kM5K'></q></dir></style></legend>

        <i id='4kM5K'><tr id='4kM5K'><dt id='4kM5K'><q id='4kM5K'><span id='4kM5K'><b id='4kM5K'><form id='4kM5K'><ins id='4kM5K'></ins><ul id='4kM5K'></ul><sub id='4kM5K'></sub></form><legend id='4kM5K'></legend><bdo id='4kM5K'><pre id='4kM5K'><center id='4kM5K'></center></pre></bdo></b><th id='4kM5K'></th></span></q></dt></tr></i><div id='4kM5K'><tfoot id='4kM5K'></tfoot><dl id='4kM5K'><fieldset id='4kM5K'></fieldset></dl></div>

          <bdo id='4kM5K'></bdo><ul id='4kM5K'></ul>
      1. <tfoot id='4kM5K'></tfoot>

      2. 将映射值复制到 STL 中的向量

        Copy map values to vector in STL(将映射值复制到 STL 中的向量)

          • <bdo id='c5yAL'></bdo><ul id='c5yAL'></ul>
            <tfoot id='c5yAL'></tfoot>

            <small id='c5yAL'></small><noframes id='c5yAL'>

                <tbody id='c5yAL'></tbody>

                  <legend id='c5yAL'><style id='c5yAL'><dir id='c5yAL'><q id='c5yAL'></q></dir></style></legend>

                  <i id='c5yAL'><tr id='c5yAL'><dt id='c5yAL'><q id='c5yAL'><span id='c5yAL'><b id='c5yAL'><form id='c5yAL'><ins id='c5yAL'></ins><ul id='c5yAL'></ul><sub id='c5yAL'></sub></form><legend id='c5yAL'></legend><bdo id='c5yAL'><pre id='c5yAL'><center id='c5yAL'></center></pre></bdo></b><th id='c5yAL'></th></span></q></dt></tr></i><div id='c5yAL'><tfoot id='c5yAL'></tfoot><dl id='c5yAL'><fieldset id='c5yAL'></fieldset></dl></div>
                • 本文介绍了将映射值复制到 STL 中的向量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  目前正在通过 Effective STL 工作.第 5 条建议通常最好使用范围成员函数而不是它们的单个元素对应物.我目前希望将地图中的所有值(即 - 我不需要键)复制到向量中.

                  Working my way through Effective STL at the moment. Item 5 suggests that it's usually preferable to use range member functions to their single element counterparts. I currently wish to copy all the values in a map (i.e. - I don't need the keys) to a vector.

                  最干净的方法是什么?

                  推荐答案

                  在这里你不能轻易使用范围,因为你从地图中得到的迭代器是指一个 std::pair,你将用来插入迭代器的地方into a vector 指的是存储在 vector 中的类型的对象,它(如果您丢弃密钥)不是一对.

                  You can't easily use a range here because the iterator you get from a map refers to a std::pair, where the iterators you would use to insert into a vector refers to an object of the type stored in the vector, which is (if you are discarding the key) not a pair.

                  我真的不认为它比显而易见的更干净:

                  I really don't think it gets much cleaner than the obvious:

                  #include <map>
                  #include <vector>
                  #include <string>
                  using namespace std;
                  
                  int main() {
                      typedef map <string, int> MapType;
                      MapType m;  
                      vector <int> v;
                  
                      // populate map somehow
                  
                      for( MapType::iterator it = m.begin(); it != m.end(); ++it ) {
                          v.push_back( it->second );
                      }
                  }
                  

                  如果我要多次使用它,我可能会将其重写为模板函数.类似的东西:

                  which I would probably re-write as a template function if I was going to use it more than once. Something like:

                  template <typename M, typename V> 
                  void MapToVec( const  M & m, V & v ) {
                      for( typename M::const_iterator it = m.begin(); it != m.end(); ++it ) {
                          v.push_back( it->second );
                      }
                  }
                  

                  这篇关于将映射值复制到 STL 中的向量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  What is the past-the-end iterator in STL C++?(STL C++ 中的最后迭代器是什么?)
                  vector::at vs. vector::operator[](vector::at 与 vector::operator[])
                  C++ equivalent of StringBuffer/StringBuilder?(C++ 等效于 StringBuffer/StringBuilder?)
                  Adding types to the std namespace(将类型添加到 std 命名空间)
                  Is the C++ std::set thread-safe?(C++ std::set 线程安全吗?)
                  How to use std::find/std::find_if with a vector of custom class objects?(如何将 std::find/std::find_if 与自定义类对象的向量一起使用?)

                    <tfoot id='WmvrC'></tfoot>
                    • <legend id='WmvrC'><style id='WmvrC'><dir id='WmvrC'><q id='WmvrC'></q></dir></style></legend>
                          <tbody id='WmvrC'></tbody>

                        <small id='WmvrC'></small><noframes id='WmvrC'>

                          <bdo id='WmvrC'></bdo><ul id='WmvrC'></ul>
                          <i id='WmvrC'><tr id='WmvrC'><dt id='WmvrC'><q id='WmvrC'><span id='WmvrC'><b id='WmvrC'><form id='WmvrC'><ins id='WmvrC'></ins><ul id='WmvrC'></ul><sub id='WmvrC'></sub></form><legend id='WmvrC'></legend><bdo id='WmvrC'><pre id='WmvrC'><center id='WmvrC'></center></pre></bdo></b><th id='WmvrC'></th></span></q></dt></tr></i><div id='WmvrC'><tfoot id='WmvrC'></tfoot><dl id='WmvrC'><fieldset id='WmvrC'></fieldset></dl></div>