0

I have a program that creates a std::vector and adds the given values to it using push_back(). The expected output is to print the vector values in default order and reverse order using an iterator and reverse iterator. The output is absolutely correct but I was just wondering if there's a better way to do this: :-

    #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;
    }

Figured out an alternate method

#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;
}
JeJo
  • 30,635
  • 6
  • 49
  • 88
Noobie
  • 39
  • 7

1 Answers1

4

You can do as follows:

Option - 1

Use range-based for loop for both inputting and printing(normally) the element of vector. And using std::for_each function, along with const reverse iterator and a lambda, reverse print the elements of the vector.

#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;
}

Option - 2

Another, complete iterator way of doing is using std::istream_iterator and std::ostream_iterator, also the algorithum functions std::copy and std::copy_n.

#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;
}

Option - 3 (Update)

After seeing your edit/update in your question, I realize that you are trying to print elements using traditional iterator-for-loop. If so, you do not need extra array int a[40];. Instead, allocate and initilze the vector g1 with size n and can input the elements using the iterator as follows:

#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;
}

Side-Notes:

  1. Do not practice coding with using namespace std;, (see the reason here.)

  2. Try to maximum avoid using global variables. In your case, definitely no need.

JeJo
  • 30,635
  • 6
  • 49
  • 88
  • 1
    Your answer would certainly better if you can add some explanation! Good job anyway! – iBug Sep 29 '18 at 05:54
  • This looks much better, just gonna figure out the push_back() implementation here lol, brb fellas – Noobie Sep 29 '18 at 05:57