2

I'm in the process of learning C++/general programming and sometimes experiment with C++11 features while doing exercises written for the older standard.

This exercise involves a vector of pointers to strings.

#include <vector>
#include <string>
#include <iostream>
int main()
{
    using std::string ;
    using std::vector ;
    using std::cout ;
    using std::endl ;

    vector<string *> v = {new string("Hello") , new string("World")} ;
    for (string * x : v) {
        cout << *x << " " ;
        delete x ;
    }
    cout << endl ;
}

I had a little difficulty figuring how to use the initialization list with this vector, but this seems to work.

This version also works:

    //...
    string s1 = "Hello" ;
    string s2 = "World" ;
    vector<string *> v = {&s1 , &s2} ;
    for (string * x : v)
        cout << *x << " " ;
    //...

It looks cleaner, and from what I've learned so far it seems like the better solution.

But I find myself wondering: is there some other way to initialize the vector without creating the strings ahead of time or having to use delete? What I read suggests the {} list should be usable universally.

And for what it's worth, this gives a rather catastrophic error:

vector<string *> v = {"Hello" , "World"} ;
Enra
  • 86
  • 1
  • 4

3 Answers3

12

Why do you use std::string* in the first place? Do you have any reason for that? I don't think so.

Use this:

std::vector<std::string> v{"Hello" , "World"};

Now, you don't need to use new. Such classes are designed precisely because you could avoid using new yourself.

Also note that there is no '=' between 'v' and '{' in the initialization above. This initialization is called uniform initialization introduced by C++11. More examples here.

Community
  • 1
  • 1
Nawaz
  • 353,942
  • 115
  • 666
  • 851
  • It does seem arbitrary, but the exercise goes on to use the arrow operator, so it probably uses pointers just for the sake of that. Then, once I tried to use the initialization list instead of push_back(), I ran into this, and now I'm mostly curious if there's another way, not really related to the original problem. – Enra May 27 '12 at 08:14
  • @Enra: You're using a bad example to learn arrow operator. Why don't you implement some class yourself, and explore how to use `->` along with many other things, which you will learn in the process. – Nawaz May 27 '12 at 08:15
  • doesn't even work on apple clang 4.2, i'm a little disappointed. – jokoon Jun 04 '13 at 00:17
3

But I find myself wondering: is there some other way to initialize the vector without creating the strings ahead of time or having to use delete?

No, to store pointers to something, you have to have this something stored somewhere else. Otherwise there will be no place for the pointer to point to.

That it feels odd is probably because this is an unusual way to store strings. If it wasn't an (artificial) exercise, you could be using a std::vector<std::string>> and get rid of your problems.

Bo Persson
  • 90,663
  • 31
  • 146
  • 203
0

Like Nawaz said, why bother using pointers to strings? Just for the sake of using the -> operator? Make a class that has a few private variables and return them using references. That's a better example of when to use the arrow operator.

James Mitch
  • 519
  • 2
  • 11
  • 26