Update container value on element modify

Sep 2, 2013 at 9:14pm
Hello,

I need "little" help.

I'm trying to make dynamic status bar. How? I will create class, where will be function add(const string text, T value)

How it should be it in code which I want.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
std::vector<string, T> cont;

void add(const string text, T value)
{
  cont.push_back(text, value);
}

int ex = 1;

add("Example", ex);

ex = 12;

for(int i = 0; i < cont.count(); i++)
  std::cout << cont[i]; // print "12" 


Thanks for reply.
Sep 2, 2013 at 10:12pm
You can use std::pair<std::string, T>

For example

std::vector<std::pair<std:;string, T>> cont;
Sep 3, 2013 at 6:18am
I tried, but not working with pair :/ same result as with vector
Sep 3, 2013 at 10:08am
use a map:

http://www.cplusplus.com/reference/map/map/?kw=map

you cannot update the value in the container like on line 12.
Only the local(?) variable ex is updated
Sep 3, 2013 at 10:37am
"status bar" usually implies GUI. GUI implies the use of existing frameworks. Frameworks tend to provide "status bar" objects.
Sep 3, 2013 at 12:19pm
keskiverto: forget status bar, I need container which refers to parent element

coder777: still return 1;
Why it is impossible?
Sep 3, 2013 at 12:54pm
sd::vector::push_back(val) stores a copy of val in the vector.

If your vector would store pointers, then each element in the vector would refer to some memory location, and could be dereferenced. The down-side is that you have to manage those memory locations somehow.

Word "parent" is often interchangeable with word "owner". While this is "just" semantics, it can affect how we communicate and approach problems.
Sep 3, 2013 at 2:16pm
Yes, you mention that what I need!

I need vector to store pointers. Is it possible or not? If not then it's really impossible to make it!

EDIT:

Thanks, after u mention that, I use google and find example. Thanks for you keskiverto!

Btw to all thanks too :)
Last edited on Sep 3, 2013 at 2:24pm
Topic archived. No new replies allowed.