有没有更好的方法使用迭代器和reverse_iterator打印矢量元素

时间:2018-09-29 05:45:22

标签: c++ c++11 iterator stdvector

我有一个创建向量的程序,并使用push_back()向其添加给定值。预期的输出是使用迭代器和反向迭代器按默认顺序和反向顺序输出矢量值。输出是绝对正确的,但我只是想知道是否有更好的方法可以做到这一点::-

    #include <iostream>
    #include <vector>
    using namespace std;
    vector<int> myvector;
    int i;
    int main()
    {
        int n;
        cin>>n;
        int input;
        for (i = 0; i < n; i++) 
        {
            cin >> input;
            myvector.push_back(input);
        }
        for (int i = 0; i < n; i++) 
        {
            cout<<" "<<myvector[i]; //prints user vector input
        }
      cout<<endl;
      typedef vector<int>::iterator iter_type;                                      
      iter_type from (myvector.begin());                                   
      iter_type until (myvector.end());                      
      reverse_iterator<iter_type> rev_until (from);                                               
      reverse_iterator<iter_type> rev_from (until);   
      while (rev_from != rev_until)
      cout << ' ' << *rev_from++;  //prints reversed vector
      return 0;
    }

找出另一种方法

#include <vector>
#include <iostream>
int main()
{
  int n;
  std::cin>>n;
  std::vector<int> g1;
  int a[40];
  std::vector<int>::iterator i;
  std::vector<int>::reverse_iterator it;
  for(int i=0;i<n;i++)
  {
    std::cin>>a[i];
    g1.push_back(a[i]);
  }
  for (auto i = g1.begin(); i != g1.end(); ++i)
        std::cout << *i << " ";
    std::cout<<"\n";
    for (auto it = g1.rbegin(); it != g1.rend(); ++it)
        std::cout << *it << " ";
  return 0;
}

1 个答案:

答案 0 :(得分:3)

您可以执行以下操作:

选项-1

range-based for loop 用于     输入和打印(通常)向量元素。并使用      std::for_each             函数,以及 const reverse iterator 和一个      lambda ,反向打印矢量的元素。

for (k = i; k <= 2 * i - 1; k++){
    System.out.println("temp k value:" + k); // k value is increament by 1
}
System.out.println ("final K value:"+k);

选项-2

另一种完整的迭代器方法是使用 std::istream_iterator std::ostream_iterator ,还有算法功能 std::copy std::copy_n

#include <algorithm>
#include <iostream>
#include <vector>

int main()
{
    int n;  std::cin >> n;
    std::vector<int> myvector(n);
    for (int& element : myvector) std::cin >> element;
    for (const int element : myvector) std::cout << element << " "; std::cout << std::endl;
    std::for_each(myvector.crbegin(), myvector.crend(), [](const int element) { std::cout << element << " "; }); //prints reversed vector
    return 0;
}

选项-3(更新

看到问题中的edit/update后,我意识到您正在尝试使用传统的 iterator-for-loop 打印元素。如果是这样,则不需要额外的数组#include <algorithm> #include <iostream> #include <vector> #include <iterator> int main() { int n; std::cin >> n; std::vector<int> myvector; myvector.reserve(n); std::copy_n(std::istream_iterator<int>(std::cin), n, std::back_inserter(myvector)); std::copy(myvector.cbegin(), myvector.cend(), std::ostream_iterator<int>(std::cout, " ")); std::cout << std::endl; std::for_each(myvector.crbegin(), myvector.crend(), [](int element){ std::cout << element << " ";}); //prints reversed vector return 0; } 。相反,分配并初始化大小为int a[40];的向量g1并可以使用迭代器输入元素,如下所示:

n

注释:

  1. 请勿使用#include <iostream> #include <vector> int main() { int n; std::cin >> n; std::vector<int> g1(n); // change here for (auto iter = g1.begin(); iter != g1.end(); ++iter) std::cin >> *iter; // change here for (auto iter = g1.cbegin(); iter != g1.cend(); ++iter) std::cout << *iter << " "; std::cout << "\n"; for (auto iter = g1.crbegin(); iter != g1.crend(); ++iter) std::cout << *iter << " "; return 0; } see the reason here.

  2. 进行编码
  3. 尽量避免使用全局变量。就你而言 绝对不需要。