Daniel Marschall
Daniel Marschall

Reputation: 3879

Iterate vector inside method

I am currently learning C++ , and I have following working code:

int main(int argc, char** argv) {
        map<unsigned int, list<mpz_class>> otp;

        // .....

        for(auto it1 = otp.begin(); it1 != otp.end(); ++it1) {
                bool first = true;
                for(auto it2 = it1->second.begin(); it2 != it1->second.end(); ++it2) {
                        if (!first) {
                                cout << ", ";
                        } else {
                                first = false;
                        }
                        cout << *it2;
                }
        }
}

I wanted to put the printing of the list into an function.

This was my try:

void prnt_list(vector it, ostream outstr, string delimiter) {
        bool first = true;
        for(auto it2 = it.begin(); it2 != it.end(); ++it2) {
                if (!first) {
                        outstr << delimiter;
                } else {
                        first = false;
                }
                outstr << *it2;
        }
}

int main(int argc, char** argv) {
    map<unsigned int, list<mpz_class>> otp;

    // .....

    for(auto it1 = otp.begin(); it1 != otp.end(); ++it1) {
        prnt_list(it1->second, cout, ", ");
    }
)

It does not compile:

error: variable or field 'prnt_list' declared void
error: missing template arguments before 'it'
error: expected primary-expression before 'outstr'
error: expected primary-expression before 'delimiter'

Then I tried this:

template <typename T>
void prnt_list<T>(vector<T> it, ostream outstr, string delimiter) {
    ...
}

But it also doesn't work.

Also, I also don't like the enforced template, because I want to allow any vector. It would be much more comfortable, if I could use the auto keyword somehow.

Upvotes: 2

Views: 124

Answers (4)

Richard Hodges
Richard Hodges

Reputation: 69864

expandind on Vlad's answer - allowing print_list to participate in an ostream << chain:

#include <iostream>
#include <list>

namespace detail {

    // it's called a simple list printer because it assumes that
    // list::value_type is printable
    template<class T, class A>
    struct simple_list_printer {
        simple_list_printer(const std::list<T, A>& list, std::string delimiter)
        : _list { list }
        , _delimiter { delimiter }
        {}

        void write(std::ostream& os) const {
            bool first = true;

            for ( const auto &obj : _list )
            {
                if ( first ) first = false;
                else os << _delimiter;

                os << obj;
            }
        }
    private:
        const std::list<T, A>& _list;
        std::string _delimiter;
    };

    // stream an simple_list_printer to any ostream
    template<class T, class A>
    inline std::ostream& operator<<(std::ostream& os, const simple_list_printer<T, A>& slp)
    {
        slp.write(os);
        return os;
    }
}

// a mockup of mpz_class
struct mpz_class {

    void write(std::ostream& os) const {
        os << "{ i am an mpz - write my members here }";
    }
};

// stream an mpz to any ostream
inline std::ostream& operator<<(std::ostream& os, const mpz_class& mpz)
{
    mpz.write(os);
    return os;
}

// construct a simple_list_printer
template<class A>
detail::simple_list_printer<mpz_class, A>
print_list(const std::list<mpz_class, A>& list, std::string delimiter = std::string { ", " })
{
    return { list, std::move(delimiter) };
}

int main()
{
    std::list<mpz_class> a { {}, {} };

    std::cout << print_list(a) << std::endl;
    std::cerr << print_list(a) << std::endl;
    std::clog << print_list(a) << std::endl;

    std::cout << print_list(a, "\n") << std::endl;
    std::cout << std::endl;
    std::cerr << print_list(a, " * ") << std::endl;
    std::clog << print_list(a, " <--> ") << std::endl;

    return 0;
}

expected output:

{ i am an mpz - write my members here }, { i am an mpz - write my members here }
{ i am an mpz - write my members here }, { i am an mpz - write my members here }
{ i am an mpz - write my members here }, { i am an mpz - write my members here }
{ i am an mpz - write my members here }
{ i am an mpz - write my members here }

{ i am an mpz - write my members here } * { i am an mpz - write my members here }
{ i am an mpz - write my members here } <--> { i am an mpz - write my members here }

Upvotes: 1

sp2danny
sp2danny

Reputation: 7644

you are sending a list to a function that expects a vector
the generic could look like this:

template <typename T>
void prnt_list(const T& it, std::ostream& outstr, const std::string& delimiter)
{
    bool first = true;
    for( auto&& obj : it ) {
        if (!first)
            outstr << delimiter;
        else
            first = false;
        outstr << obj;
    }
}

also note that << has to be defined for mpz_class

it requires the headers <string> and <iostream>

Upvotes: 1

Vlad from Moscow
Vlad from Moscow

Reputation: 310930

The function can be defined the following way

std::ostream & prnt_list( const std::list<mpz_class> &lst, 
                          std::ostream &os = std::cout, 
                          const char *delimiter = ", " ) 
{
    bool first = true;

    for ( const auto &obj : lst ) 
    {
        if ( first ) first = false;
        else os << delimiter;

        os << obj;
    }

    return os;
}

and called like

for ( const auto &p : otp ) prnt_list( p.second ) << std::endl;

You could write a general function for any container. For example

template <class Container>
std::ostream & prnt_list( const Container &container, 
                          std::ostream &os = std::cout, 
                          const char *delimiter = ", " ) 
{
    bool first = true;

    for ( const auto &obj : container ) 
    {
        if ( first ) first = false;
        else os << delimiter;

        os << obj;
    }

    return os;
}

As for your code then at least identifier vector is not declared and streams are not copyable.

Upvotes: 3

marcinj
marcinj

Reputation: 49976

The problem is :

map<unsigned int, list<mpz_class>> otp;

has as second a list, and as a first argument to prnt_list you have vector. Why?

To fix those errors use following signature for prnt_list:

template<typename T>
void prnt_list(list<T>& it, ostream& outstr, string delimiter) {

Upvotes: 1

Related Questions