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

    • <bdo id='oixQQ'></bdo><ul id='oixQQ'></ul>

      <tfoot id='oixQQ'></tfoot>
    1. <legend id='oixQQ'><style id='oixQQ'><dir id='oixQQ'><q id='oixQQ'></q></dir></style></legend>

    2. <i id='oixQQ'><tr id='oixQQ'><dt id='oixQQ'><q id='oixQQ'><span id='oixQQ'><b id='oixQQ'><form id='oixQQ'><ins id='oixQQ'></ins><ul id='oixQQ'></ul><sub id='oixQQ'></sub></form><legend id='oixQQ'></legend><bdo id='oixQQ'><pre id='oixQQ'><center id='oixQQ'></center></pre></bdo></b><th id='oixQQ'></th></span></q></dt></tr></i><div id='oixQQ'><tfoot id='oixQQ'></tfoot><dl id='oixQQ'><fieldset id='oixQQ'></fieldset></dl></div>
      1. 依赖范围;前面需要typename;

        时间:2023-10-07
        <tfoot id='uykcq'></tfoot>
        <i id='uykcq'><tr id='uykcq'><dt id='uykcq'><q id='uykcq'><span id='uykcq'><b id='uykcq'><form id='uykcq'><ins id='uykcq'></ins><ul id='uykcq'></ul><sub id='uykcq'></sub></form><legend id='uykcq'></legend><bdo id='uykcq'><pre id='uykcq'><center id='uykcq'></center></pre></bdo></b><th id='uykcq'></th></span></q></dt></tr></i><div id='uykcq'><tfoot id='uykcq'></tfoot><dl id='uykcq'><fieldset id='uykcq'></fieldset></dl></div>
          <tbody id='uykcq'></tbody>

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

            <bdo id='uykcq'></bdo><ul id='uykcq'></ul>

              1. <legend id='uykcq'><style id='uykcq'><dir id='uykcq'><q id='uykcq'></q></dir></style></legend>

                • 本文介绍了依赖范围;前面需要typename;的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我想创建一个模板如下.我想从向量 vec1 中删除项目列表.而我要删除的项目的索引存储在index_list中.

                  I want to create a template as follows. I want to delete a list of items from vector vec1. And the indexes of the items I want to delete are stored in index_list.

                  #include <vector>
                  
                  using namespace std;
                  
                  template <typename a_type>
                  bool vector_remove(vector< a_type > & vec1, vector< int > index_list)
                  {
                      //index_list is sorted in order from small to large.
                  
                      if(index_list.size() > vec1.size())
                      {
                          cout << "ERROR in 'vector_remove()': index_list is longer than vec1."<<endl;
                          return false;
                      }
                      if(index_list.size() == vec1.size())
                      {
                          vec1.clear();
                          return true;
                      }
                      vector< int >::iterator ind_pt = index_list.begin();
                      vector< a_type >::iterator vec1_pre = vec1.begin();
                      vector< a_type >::iterator vec1_pos = vec1.begin();
                      int vec1_ind = 0;
                      while(ind_pt != index_list.end() && vec1_pos != vec1.end())
                      {
                          if(*ind_pt == vec1_ind)
                          {
                              ind_pt ++;
                              vec1_pos ++;
                              vec1_ind ++;
                          }
                          else if( *ind_pt > vec1_ind )
                          {
                              *(vec1_pre) = *(vec1_pos);
                              vec1_pos ++;
                              vec1_pre ++;
                              vec1_ind ++;
                          }
                          else
                          {
                              cout << "ERROR in 'vector_remove'." <<endl;
                              return false;
                          }
                      }
                      while(vec1_pos != vec1.end())
                      {
                          *(vec1_pre) = *(vec1_pos);
                          vec1_pos ++;
                          vec1_pre ++;
                      }
                      // the above codes are to put all the rejected elements to the end of the vec1.
                  
                      // pop back all the rejected elements.
                      while(vec1_pre != vec1.end() )
                      {
                          vec1.pop_back();
                      }
                  
                      return true;
                  }
                  

                  但是它返回了很多错误:

                  But it returns a lot of errors:

                  In file included from demo.cpp:3:0:
                  my_vector.h: In function ‘bool vector_remove(std::vector<a_type>&, std::vector<int>)’:
                  my_vector.h:21:2: error: need ‘typename’ before ‘std::vector<a_type>::iterator’ because ‘std::vector<a_type>’ is a dependent scope
                    vector< a_type >::iterator vec1_pre = vec1.begin();
                    ^
                  my_vector.h:21:29: error: expected ‘;’ before ‘vec1_pre’
                    vector< a_type >::iterator vec1_pre = vec1.begin();
                                               ^
                  my_vector.h:22:2: error: need ‘typename’ before ‘std::vector<a_type>::iterator’ because ‘std::vector<a_type>’ is a dependent scope
                    vector< a_type >::iterator vec1_pos = vec1.begin();
                    ^
                  my_vector.h:22:29: error: expected ‘;’ before ‘vec1_pos’
                    vector< a_type >::iterator vec1_pos = vec1.begin();
                                               ^
                  my_vector.h:24:38: error: ‘vec1_pos’ was not declared in this scope
                    while(ind_pt != index_list.end() && vec1_pos != vec1.end())
                                                        ^
                  my_vector.h:34:6: error: ‘vec1_pre’ was not declared in this scope
                      *(vec1_pre) = *(vec1_pos);
                        ^
                  my_vector.h:45:8: error: ‘vec1_pos’ was not declared in this scope
                    while(vec1_pos != vec1.end())
                          ^
                  my_vector.h:47:5: error: ‘vec1_pre’ was not declared in this scope
                     *(vec1_pre) = *(vec1_pos);
                       ^
                  my_vector.h:54:8: error: ‘vec1_pre’ was not declared in this scope
                    while(vec1_pre != vec1.end() )
                  

                  谁能帮我解决这个问题?

                  could anyone help me solve this?

                  推荐答案

                  编译器说

                  my_vector.h:21:2: 错误:之前需要‘typename’‘std::vector::iterator’ 因为‘std::vector’是一个依赖范围

                  my_vector.h:21:2: error: need ‘typename’ before ‘std::vector::iterator’ because ‘std::vector’ is a dependent scope

                  所以你需要写

                  typename vector< a_type >::iterator vec1_pre = vec1.begin();
                  typename vector< a_type >::iterator vec1_pos = vec1.begin();
                  

                  见哪里和为什么我必须放模板"?和类型名称"关键字? 其背后的原因.

                  最后一句话:在 C++11 中你可以使用 auto 而不必再想了:

                  One last remark: In C++11 you can use auto and don't have to think anymore:

                  auto vec1_pre = vec1.begin();
                  auto vec1_pos = vec1.begin();
                  

                  这篇关于依赖范围;前面需要typename;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:在成对的向量中按键查找成对 下一篇:插入具有没有复制构造函数的对象的向量

                  相关文章

                  最新文章

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

                  <legend id='agac7'><style id='agac7'><dir id='agac7'><q id='agac7'></q></dir></style></legend><tfoot id='agac7'></tfoot>

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

                    • <bdo id='agac7'></bdo><ul id='agac7'></ul>