-7

From what I understand you create a list by assigning a variable multiple values.

int tileNorth;
tileNorth = 1, 0;

But I do not know how to print out which values I want.

std::cout<<tileNorth(value one)<<tileNorth(value two);

What I am trying to do is print out what values I need from the list.

Andrew
  • 1
  • 1
  • 2
    *"you create a list by assigning a variable multiple values"* That's not true. You're confusing C++ with some other language. – HolyBlackCat Oct 23 '18 at 21:49
  • 2
    The simple answer is that `tileNorth = 1, 0` is not a list. Technically speaking it's what's known as the comma operator and has nothing to do with lists. If you want to know more, see https://en.wikipedia.org/wiki/Comma_operator. However, there's probably not a lot of mileage in diving too deeply into that — instead, I'd recommend picking up a good modern introductory C++ textbook. – NPE Oct 23 '18 at 21:50
  • @NeilButterworth It initializes `tileNorth` with `1`, actually; the comma operator has lower precedence than the assignment operator. – Justin Oct 23 '18 at 21:51
  • @NeilButterworth, nope, wrong. It assigns 1 to `tileNorth`, and than `0` is a result of the expression. – SergeyA Oct 23 '18 at 21:52
  • 2
    Use `std::list` to have list structures in C++. – SergeyA Oct 23 '18 at 21:54
  • 1
    @SergeyA ... are you joking? Why would you recommend `std::list` if you want a list? Surely you mean `std::vector`? – Justin Oct 23 '18 at 21:54
  • @Justin, `std::vector` is not a list from CS standpoint (doesn't fit into complexity requirements), so I can't recommend it as a substitute for list. – SergeyA Oct 23 '18 at 21:57
  • 1
    @SergeyA I think the confusion is over the word "list." I learned "list" to mean "anything fulfilling the interface of a list", not specifically a linked list.So `std::vector` is a "list" because it's an array list, but it's not a linked list – Justin Oct 23 '18 at 21:59
  • @Justin it depends on the context. In CS, a list is a very specific term which has certain complexity requirements imposed, and `std::list` fits those. In other contexts it might mean other things. Since I do not know which context was used by OP, both readings are appropriate. And in my opinion, the time we spend in this discussion is better spent in VTC/Deleting this low quality question. – SergeyA Oct 23 '18 at 22:00

1 Answers1

2

This should do the job. The comments above are accurate.

#include <iostream>
#include <list>

int main() {

    std::list<int> listOfInts{1,2,3};

    for(auto element: listOfInts) {
        std::cout << element << " ";
    }
    std::cout << std::endl;
    return 0;
}

Here is an alternative solution which uses vector. The code is almost identical, except that I have added a random number generator to push 10 random integers between 1 and 10 onto the vector.

There was a lot of discussion about whether a list<> should be used. The differences between a c++ list and vector are discussed in many places, including here: thispointer.com list vs vector and here:stackoverflow-is it ever a good idea to use a list. Most importantly, a list favors quick insertions and removals from the middle of the list whereas the vector favors quick retrieval of values, quick searches, and just about everything else you may want to do with a container.

Generally, the list<> is assumed to be a doubly linked list whereas the vector is assumed to be a "dynamically" growing array. By default, the vector is probably preferred, but analytics is the only way to truly answer the question.

#include <iostream>
#include <vector>
#include <cstdlib>
#include <ctime>


int main() {

    srand((unsigned)time(0));
    int random_integer = rand();

    std::vector<int> vectorOfInts;

    for(int i = 0; i < 10; i++) {
        vectorOfInts.push_back(rand()%10 + 1);
    }

    for(auto element: vectorOfInts) {
        std::cout << element << " ";
    }
    std::cout << std::endl;
    return 0;
}
Nimantha
  • 6,405
  • 6
  • 28
  • 69
Gardener
  • 2,591
  • 1
  • 13
  • 22
  • 1
    I wonder why would people downvote this answer. I see nothing inherently wrong with it. – SergeyA Oct 23 '18 at 21:58
  • @SergeyA: Not sure. Sometimes we forget that teaching does not always mean holding back information. Some of the comments are condescending. -6 for a post from a newbie! That should never happen. I agree that a vector is a better way to handle this, but the OP asked for a list. Knowing that there are lists, vectors, sets, and on and on is good, but the CS student has to learn all of them to understand which one is better. – Gardener Oct 23 '18 at 22:16
  • You may also want to give an example using `std::vector`. – Thomas Matthews Oct 23 '18 at 23:03
  • I have added a vector example in response to the request by @ThomasMatthews. Enjoy. – Gardener Oct 23 '18 at 23:29