user3819671
user3819671

Reputation: 35

C++ program using standard library containers

New to standard library containers and my teacher asked a question like this at the end of lecture today for us to learn them

Write a C++ program that reads int from standard input until the end of a file and then prints them in reverse order--one per line. Use the standard library containers to store the data. No use of the new operator.

How would I do this if I had to use the standard library containers?

int main()
int i = 0;
{int array[];

vector<int> integers (array, array+ array.size)
for (i=0; i<array.size, i++) 
   cin>>a[i];

vector<int>::const_reverse_iterator reverse iterator;

^^this is my pseudocode so far, still getting there. Any feedback?

Upvotes: 2

Views: 177

Answers (2)

user3920237
user3920237

Reputation:

I cannot speak for others but I think that careless usage of the acronym "STL" is intellectually dishonest. If you want to refer to the "C++ standard library", then say so. The abbreviation can mean many different things, including the standard library, the original STL implementation (which is over a decade old), or any of its ports. For more reading, see What's this STL vs. “C++ Standard Library” fight all about?

In terms of learning C++, many students seem to neglect one of the most important (and very expensive) resources they have, which is their school. If you have a problem, always ask your teacher first. Ask the teacher's aide. Ask other students. Find an on-campus tutor. Hit the library. If your library doesn't have a book you want, request that it be delivered to your campus. But I'll give you the benefit of the doubt and assume you have done that already.

If you want a reference, try cppreference. For a beginner, it is not very easy to parse, although it considerably simplifies the language from the standard. If you want a tutorial, try any one of Bjarne Stroustrup's million of books (exaggeration). The C++ Programming Language (4th Edition) is a good choice. The ISO C++ website also lists popular compilers, online compilers and a small list of books by C++ personalities (Herb Sutter, Scott Meyers, etc.) There is also Marshall Cline's C++-faq (various authors).

People also seem to forget that StackOverflow is a resource. People have spent hundreds of hours contributing to the , and even just sorting C++ questions by votes gives plenty of high quality content to go through. If that isn't enough, sort the user page by reputation and look for users who are experts in C++ and read their questions and answers. You can't say that you haven't been able to find the answer to your problem after doing all that.

Nothing I have wrote so far constitutes an answer to the question, so let's tackle your problem.

Your current code shows:

int main()
int i = 0;
{
  int array[];

  vector<int> integers (array, array+ array.size)
  for (i=0; i<array.size, i++) 
     cin>>a[i];

  vector<int>::const_reverse_iterator reverse iterator;

The first issue is your K&R style declaration. int i = 0 needs to inside main. Then, you have an array of unknown bound. In this context, it is not allowed. Just start off with a std::vector, an array isn't needed here. And finally, you don't need a reverse_iterator and I'll show you why in a second.

#include <iostream>
#include <vector>

int main() {
    std::vector<int> v;
    int current_num;
    while (std::cin >> current_num)
        v.push_back(current_num); 
}

This idiom, std::cin >> current_num, is useful because it's concise and the loop will exit gracefully (i.e., bad user input or EOF). Please see Why is iostream::eof inside a loop condition considered wrong? for a longer explanation.

Now you want to print the integers in reverse order. You have three ways of doing this:

std::sort(v.rbegin(), v.rend());
std::reverse(v.begin(), v.end()); // #include <algorithm>

Then loop through and print each element. Or use a loop that iterates through the vector backwards while printing each element. The two methods above are virtually equivalent, but I included rbegin() because it returns a reverse iterator, demonstrating that you don't have to deal with a naked iterator.

I won't bother to provide longer explanations because it's 3:30 in the morning but hopefully that will get you started. Good luck.

Upvotes: 4

MateuszZawadzki
MateuszZawadzki

Reputation: 149

If you are starting, best is std::vector.

vector<int> v;
int temp = 0;

while(cin>>temp)
{
    v.push_back(temp);
}

for(int i=0; i<v.size(); i++)
    cout<<v[v.size()-1-i] <<endl;

Upvotes: 0

Related Questions