0

If there is the following vector1:

vector<string*> cont;   // cont[0] == "0"

where pointers to strings are named either l or r; are sequentially added like so:

string r* = new string("1");
cont.emplace_back(r);

or:

string l* = new string("-1");
cont.emplace_back(l);

For example: if there is a direction to a node given like: "lrlrrr".

Is there a way to search through the vector using the string names, l and r, as "element id" rather than string content2?

Note: I've researched finding a vector element by native property, however, I'm interested if there is alternative way.


1. The vector stores sequentially (level by level) the nodes of a binary tree, where each left node's,l, value is: parent value - 1 and each right node's, r, value is: parent value + 1.

2. Comparing current and previous node values determines if current node left or right.

Community
  • 1
  • 1
Ziezi
  • 6,375
  • 3
  • 39
  • 49
  • Why are you storing pointers to string? You also have some typos. – NathanOliver Oct 19 '15 at 12:41
  • I am storing pointers to _objects_. I just wanted to make more concrete example without cluttering the question with code that has nothing to do with it. – Ziezi Oct 19 '15 at 12:44
  • You can't, because there is no connection between what you call the "object name" and the object stored in the vector. If you can establish such a connection, it may be possible. – juanchopanza Oct 19 '15 at 12:59
  • `string r* = new string("1");` is also invalid syntax, regardless of what `string` is (`std::string`, a pointer to `char`, etc). – Peter Oct 26 '15 at 12:45
  • Although `string` is purely for illustrative purpose, you are probably right . – Ziezi Oct 26 '15 at 12:52

2 Answers2

2

It is generally weird to use pointers to string in C++, since string internally contains a pointer to char giving a double indexation. But in this use case, it could make sense, if you store pointers to the same constant objects:

static string _r = "1";
static string _l = "-1";

const string * r = &_r;
const string * l = &_l;

then you could do

cont.emplace_back(r);

or

cont.emplace_back(l);

Because when iterating the vector of pointers you can do if (cont[i] == r) ...

If you really build new different objects on each step, storing pointers would only make sense if you need polymorphism, but it would be hard to test as identity if you do not have a know set of possible objects.

Serge Ballesta
  • 143,923
  • 11
  • 122
  • 252
  • I am using pointers because my objects (in the question `sting`s are used for simplification) are derived from a a base class that does not allow object copy. – Ziezi Oct 19 '15 at 13:17
  • @simplicisveritatis: But do you have a know set of them (l/r) or is if more complex? – Serge Ballesta Oct 19 '15 at 13:19
  • Yes, I have the _root_ value of `0` to compare against and existing binary tree stored in a vector to whose nodes I have to navigate using commands in the form: `lrlrlr`. – Ziezi Oct 19 '15 at 13:53
1

Is there a way to search through the vector using the string names?

No, because the names, l or r, are local variables1 that get destroyed after the string is stored in the vector. Once stored, the vector indexes become "names" of the stored strings.


1. l and r are rvalues and their addresses can not be obtained after the execution of the function that contains them.

Ziezi
  • 6,375
  • 3
  • 39
  • 49