How to make a list C++ -
so have make program stores input piece piece, until -1 input. example of input 1 1 1 0 3 5 3 -1. , -1 not recorded. plays list you, bacwards, output 3 5 3 0 1 1 1
to need make list of objects, have no clue how declare them, or how add/remove items them. how do this?
you need place store values can grow values read.
the simpler std::vector, std::list
or std::deque
whatever bidirectional container game.
then need loop values save them container (the push_back
method has purpose), , loop getting values container end , printing them.
this can done using iterators or using indexes, depending on container , on own specific needs.
this may possibility:
#include <vector> #include <iostream> template<class v, class s> void read_numbers(v& v, s& s, n end) { n x=0; while(x << s) { if(x==end) return; v.push_back(x); } std::cerr << "error: bad reading" << std::endl; } template<class v, class s> void write_numbers(const v& v, s& s) { for(auto i=s.rbegin(); i!=s.rend(); ++i) std::cout << *i << ' '; std::cout << std::endl; } int main() { std::vector<int> nums; read_numbers(nums, std::cin, -1); } write_numbers(nums, std::cout); return 0; }
Comments
Post a Comment