1

I just started to study c++ and I did not get to understand the list. I have some code using the array.

#include <iostream>
#include <string>

using namespace std;

class test {
public:
string a,b;
};

int main(){
test param [3]; 
param[0].a = "asdf";
param[0].b = "ghjk";
param[1].a = "qwer";
param[1].b = "tyui";
param[2].a = "zxcv";
param[2].b = "vbnm";
cout << "param\ta\tb\n";
for(int i = 0; i<3; i++){
cout << i << "\t" << param[i].a <<
"\t" << param[i].b << endl;
}
}

How to realize same code using the list? Below are my attempts to do this:

#include <list>
...
list <test> param;
param.push_back();
param.back().a = "asdf";
param.back().b = "ghjk";
...

second try:

...
test o; 
o.a = "asdf"; o.b = "ghjk";
param.push_back(o);
...

As you know, none of them works...

2
  • 2
    Why use std::list instead of std::vector? Commented Feb 15, 2014 at 10:12
  • 1
    As for your problem, just saying "none of them works" isn't much of a description. Please elaborate on what's wrong, including showing expected and actual output for example. Commented Feb 15, 2014 at 10:13

4 Answers 4

2

First make a object of the class test by

test temp

Then initialize temp properly

temp.a="alpha";
temp.b="beta";

Now push the temp in the list

param.push_back(temp);

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you for your answer. Can you show same code using a vector?
@OrangeFox not too much changes required for vector. Just change the declaration of param as vector<test> param; and rest of code is same. And also don't forget to include vectors by #include<vector>
2

You need to push test objects into the list. For example:

param.push_back(test());
param.back().a = "asdf";
param.back().b = "ghjk";

or

param.push_back({"asdf", "ghjk"});
param.push_back({ "qwer", "tyui" });
param.push_back({ "zxcv", "vbnm" });

Note that an std::list is a doubly-linked list, and does not support random access. As such, it is not necessarily a good replacement for an array. You might want to have a look at std::vector.

4 Comments

Why not emplace_back() instead of the second way?
@Manu343726 I am not sure there is anything to gain.
Well, emplace_back() avoid one copy, isn't? It just initialices the object allocated on the list, instead of passing an object and copying it on the list.
@Manu343726 Not really, because there isn't a to parameter constructor. So we need to pass a temporary test object anyway, which gets moved with push_back.
0
test test1;
test test2;
test test3;

test1.a = "asdf";
test1.b = "ghjk";
test2.a = "qwer";
test2.b = "tyui";
test3.a = "zxcv";
test3.b = "vbnm";

param.push_back(test1);
param.push_back(test2);
param.push_back(test3);

Comments

0

For example

#include <iostream>
#include <string>
#include <list>

class test 
{
public:
    std::string a, b;
};

int main()
{
    std::list<test> l;
    l.push_back( { "asdf", "ghjk" } );
    l.push_back( { "qwer", "tyui" } );
    l.push_back( { "zxcv", "vbnm" } );

    std::cout << "param\ta\tb\n";
    int i = 0;
    for ( const test &t : l )
    {
        std::cout << i++ << '\t' << t.a 
                         << '\t' << t.b << std::endl;
    }
}

Or

#include <iostream>
#include <string>
#include <list>

class test 
{
public:
    std::string a, b;
};

int main()
{
    std::list<test> l;

    l.push_back( test() );
    l.back() = { "asdf", "ghjk" };
    l.push_back( test() );
    l.back() =  { "qwer", "tyui" };
    l.push_back( test() );
    l.back() = { "zxcv", "vbnm" };

    std::cout << "param\ta\tb\n";
    int i = 0;
    for ( const test &t : l )
    {
        std::cout << i++ << '\t' << t.a 
                       << '\t' << t.b << std::endl;
    }
}

Or

#include <iostream>
#include <string>
#include <list>

class test 
{
public:
    std::string a, b;
};

int main()
{
    std::list<test> l;

    l.push_back( test() );
    l.back().a = "asdf"; l.back().b = "ghjk";
    l.push_back( test() );
    l.back().a = "qwer"; l.back().b = "tyui";
    l.push_back( test() );
    l.back().a = "zxcv"; l.back().b = "vbnm";

    std::cout << "param\ta\tb\n";
    int i = 0;
    for ( const test &t : l )
    {
        std::cout << i++ << '\t' << t.a 
                       << '\t' << t.b << std::endl;
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.