5

I was trying to initialize a list of strings in c++11 using the following code, and its failing with various reasons. The error says that I need to use constructor to initialize the list, should I use something like list<string> s = new list<string> [size] ? What am I missing here?

#include<string>
#include<list>
#include<iostream>
using namespace std;

int main() {
      string s = "Mark";
      list<string> l  {"name of the guy"," is Mark"};
      cout<<s<<endl;
      int size = sizeof(l)/sizeof(l[0]);
      for (int i=0;i<size;i++) {
             cout<<l[i]<<endl;
      }
      return 0;
 }

I/O is

 strtest.cpp:8:47: error: in C++98 ‘l’ must be initialized by constructor, not 
 by ‘{...}’
 list<string> l  {"name of the guy"," is Mark"};
Aparna Chaganti
  • 599
  • 2
  • 5
  • 15

4 Answers4

11

You are using a compiler of c++98 instead of c++11.using this if you are using gcc

g++ -std=c++11 -o strtest strtest.cpp

you can replace c++11 with gnu++11

K.Juce
  • 253
  • 1
  • 6
  • Thanks, I hardly realized that I was not using c++11, I guess I was scared at the amount of errors I saw and didn't catch this one – Aparna Chaganti Jun 03 '17 at 05:34
10

List initializers are only available in C++11. To use C++11 you probably have to pass a flag to the compiler. For GCC and Clang this is -std=c++11.

Also, std::list does not provide a subscript operator. You could either use a std::vector as in the other answer or you use a range-based for loop to iterate over the list.

Some more hints:

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

int main() {
  std::string s = "Mark";
  std::list<std::string> l {"name of the guy"," is Mark"};

  for (auto const& n : l)
    std::cout << n << '\n';
}
Henri Menke
  • 10,705
  • 1
  • 24
  • 42
0

The biggest problem here is that you are using lists. In C++ lists are doubly linked lists, as such the [] doesn't make any sense. You should be using vectors instead.

I'd try:

#include<string>
#include<vector>
#include<iostream>
using namespace std;

int main() {
      string s = "Mark";
      vector<string> l = {"name of the guy"," is Mark"};
      cout<<s<<endl;
      for (int i=0;i<l.size();i++) {
             cout<<l[i]<<endl;
      }
      return 0;
 }

instead

EDIT: as others pointed out, make sure you are compiling with c++ 11 and not c++ 98

Makogan
  • 8,208
  • 7
  • 44
  • 112
  • Thanks!, what would be use case for using lists then? Can you please explain only if its not too complex? – Aparna Chaganti Jun 03 '17 at 05:35
  • Well do you know what a linked list is? – Makogan Jun 03 '17 at 05:35
  • I do, its just that the term "list" is so straightforward and compelling for me to use it in a "regular" context. I guess I was asking why would some one name a doubly linked list as just "list"? – Aparna Chaganti Jun 03 '17 at 05:37
  • @AparnaChaganti The answers to the following question should help: [vector vs. list in STL](https://stackoverflow.com/q/2209224/445976) – Blastfurnace Jun 03 '17 at 05:43
0

Well the answer to this problem is to just simply copy the contents of one list into another list hope it helps :)

Aditya Aggarwal
  • 159
  • 1
  • 9