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.

5
  • Why are you storing pointers to string? You also have some typos. Commented Oct 19, 2015 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. Commented Oct 19, 2015 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. Commented Oct 19, 2015 at 12:59
  • string r* = new string("1"); is also invalid syntax, regardless of what string is (std::string, a pointer to char, etc). Commented Oct 26, 2015 at 12:45
  • Although string is purely for illustrative purpose, you are probably right . Commented Oct 26, 2015 at 12:52

2 Answers 2

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.

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

3 Comments

I am using pointers because my objects (in the question stings are used for simplification) are derived from a a base class that does not allow object copy.
@simplicisveritatis: But do you have a know set of them (l/r) or is if more complex?
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.
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.

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.