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

      1. <small id='8heIT'></small><noframes id='8heIT'>

      2. <legend id='8heIT'><style id='8heIT'><dir id='8heIT'><q id='8heIT'></q></dir></style></legend>

        如何使用迭代器在向量中导航?(C++)

        时间:2023-09-15

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

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

            <tfoot id='M8VTK'></tfoot>

                  <tbody id='M8VTK'></tbody>
                  <bdo id='M8VTK'></bdo><ul id='M8VTK'></ul>

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

                  本文介绍了如何使用迭代器在向量中导航?(C++)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  目标是访问字符串向量的第 n 个"元素,而不是使用 [] 运算符或at"方法.据我了解,迭代器可用于在容器中导航,但我以前从未使用过迭代器,而且我正在阅读的内容令人困惑.

                  The goal is to access the "nth" element of a vector of strings instead of the [] operator or the "at" method. From what I understand, iterators can be used to navigate through containers, but I've never used iterators before, and what I'm reading is confusing.

                  如果有人能给我一些有关如何实现这一目标的信息,我将不胜感激.谢谢.

                  If anyone could give me some information on how to achieve this, I would appreciate it. Thank you.

                  推荐答案

                  你需要使用 begin 和 <vectorend方法> 类,它返回分别引用第一个和最后一个元素的迭代器.

                  You need to make use of the begin and end method of the vector class, which return the iterator referring to the first and the last element respectively.

                  using namespace std;  
                  
                  vector<string> myvector;  // a vector of stings.
                  
                  
                  // push some strings in the vector.
                  myvector.push_back("a");
                  myvector.push_back("b");
                  myvector.push_back("c");
                  myvector.push_back("d");
                  
                  
                  vector<string>::iterator it;  // declare an iterator to a vector of strings
                  int n = 3;  // nth element to be found.
                  int i = 0;  // counter.
                  
                  // now start at from the beginning
                  // and keep iterating over the element till you find
                  // nth element...or reach the end of vector.
                  for(it = myvector.begin(); it != myvector.end(); it++,i++ )    {
                      // found nth element..print and break.
                      if(i == n) {
                          cout<< *it << endl;  // prints d.
                          break;
                      }
                  }
                  
                  // other easier ways of doing the same.
                  // using operator[]
                  cout<<myvector[n]<<endl;  // prints d.
                  
                  // using the at method
                  cout << myvector.at(n) << endl;  // prints d.
                  

                  这篇关于如何使用迭代器在向量中导航?(C++)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:如何在不循环的情况下将数组的内容复制到 C++ 下一篇:为什么我更喜欢使用 vector 来 deque

                  相关文章

                  最新文章

                    <bdo id='yhLKi'></bdo><ul id='yhLKi'></ul>
                  <legend id='yhLKi'><style id='yhLKi'><dir id='yhLKi'><q id='yhLKi'></q></dir></style></legend>

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

                    1. <small id='yhLKi'></small><noframes id='yhLKi'>