update structure in a vector of structures
-
Well, this is kinda maybe wrong name for a question, but... Here is what i need to do (i got no idea how, for now): Here is a structure:
typedef struct _S
{
int a;
std::string b;
} S;and here is a vector creation:
std::vector Vec;
S s;
s.a = 1;
s.b = "hi";
Vec.push_back(s);....
S s;
s.a = 10;
s.b = "something";
Vec.push_back(s);So now there are 10 or something structures in a vector. The question is: I need to be able to update values of any of these structures, while they are still in their positions inside my vector. It is like, for example, i need to update string value in structure which has "a" integer = 10, so in this case in last inserted structure i need to change string value from "something" to, for example, "nothing". How to accomplish such thing? Or maybe i should use something else as a container and not a vector?
011011010110000101100011011010000110100101101110 0110010101110011
-
Well, this is kinda maybe wrong name for a question, but... Here is what i need to do (i got no idea how, for now): Here is a structure:
typedef struct _S
{
int a;
std::string b;
} S;and here is a vector creation:
std::vector Vec;
S s;
s.a = 1;
s.b = "hi";
Vec.push_back(s);....
S s;
s.a = 10;
s.b = "something";
Vec.push_back(s);So now there are 10 or something structures in a vector. The question is: I need to be able to update values of any of these structures, while they are still in their positions inside my vector. It is like, for example, i need to update string value in structure which has "a" integer = 10, so in this case in last inserted structure i need to change string value from "something" to, for example, "nothing". How to accomplish such thing? Or maybe i should use something else as a container and not a vector?
011011010110000101100011011010000110100101101110 0110010101110011
Ok, i am so stupid =/. Completely forgot about good old iteration with "int i".
typedef struct _S
{
int a;
std::string str;
}
S;int main( void)
{
std::vector<S> Vec;
S s;
s.a = 1; s.str = "hi";
Vec.push_back(s);
s.a = 2; s.str = "lol";
Vec.push_back(s);
s.a = 3; s.str = "nothing";
Vec.push_back(s);std::vector<S>::iterator it; for(it = Vec.begin(); it != Vec.end(); it++) { cout << it->a << " : " << it->str.c\_str() << endl; } for(unsigned int i = 0; i < Vec.size(); i++) { Vec\[i\].str = "BOOM!"; } for(it = Vec.begin(); it != Vec.end(); it++) { cout << it->a << " : " << it->str.c\_str() << endl; }
}
011011010110000101100011011010000110100101101110 0110010101110011
-
Ok, i am so stupid =/. Completely forgot about good old iteration with "int i".
typedef struct _S
{
int a;
std::string str;
}
S;int main( void)
{
std::vector<S> Vec;
S s;
s.a = 1; s.str = "hi";
Vec.push_back(s);
s.a = 2; s.str = "lol";
Vec.push_back(s);
s.a = 3; s.str = "nothing";
Vec.push_back(s);std::vector<S>::iterator it; for(it = Vec.begin(); it != Vec.end(); it++) { cout << it->a << " : " << it->str.c\_str() << endl; } for(unsigned int i = 0; i < Vec.size(); i++) { Vec\[i\].str = "BOOM!"; } for(it = Vec.begin(); it != Vec.end(); it++) { cout << it->a << " : " << it->str.c\_str() << endl; }
}
011011010110000101100011011010000110100101101110 0110010101110011
You can still use iterators
std::vector<S>::iterator it;
for(it = Vec.begin(); it != Vec.end(); it++)
{
cout << "Before: "<< it->a << " : " << it->str.c_str() << endl;
it->str = "New string value";
cout << "After: "<< it->a << " : " << it->str.c_str() << endl;
}The iterator can be thought of as a pointer to your elements. There is no need to use indexing syntax just to update your data.
-
Well, this is kinda maybe wrong name for a question, but... Here is what i need to do (i got no idea how, for now): Here is a structure:
typedef struct _S
{
int a;
std::string b;
} S;and here is a vector creation:
std::vector Vec;
S s;
s.a = 1;
s.b = "hi";
Vec.push_back(s);....
S s;
s.a = 10;
s.b = "something";
Vec.push_back(s);So now there are 10 or something structures in a vector. The question is: I need to be able to update values of any of these structures, while they are still in their positions inside my vector. It is like, for example, i need to update string value in structure which has "a" integer = 10, so in this case in last inserted structure i need to change string value from "something" to, for example, "nothing". How to accomplish such thing? Or maybe i should use something else as a container and not a vector?
011011010110000101100011011010000110100101101110 0110010101110011
Hi, If the interger values are going to be unique then you can go for,
std::map<int, struct S>
Do your Duty and Don't expect the Result
-
You can still use iterators
std::vector<S>::iterator it;
for(it = Vec.begin(); it != Vec.end(); it++)
{
cout << "Before: "<< it->a << " : " << it->str.c_str() << endl;
it->str = "New string value";
cout << "After: "<< it->a << " : " << it->str.c_str() << endl;
}The iterator can be thought of as a pointer to your elements. There is no need to use indexing syntax just to update your data.
-
Hi, If the interger values are going to be unique then you can go for,
std::map<int, struct S>
Do your Duty and Don't expect the Result
-
Well, this is kinda maybe wrong name for a question, but... Here is what i need to do (i got no idea how, for now): Here is a structure:
typedef struct _S
{
int a;
std::string b;
} S;and here is a vector creation:
std::vector Vec;
S s;
s.a = 1;
s.b = "hi";
Vec.push_back(s);....
S s;
s.a = 10;
s.b = "something";
Vec.push_back(s);So now there are 10 or something structures in a vector. The question is: I need to be able to update values of any of these structures, while they are still in their positions inside my vector. It is like, for example, i need to update string value in structure which has "a" integer = 10, so in this case in last inserted structure i need to change string value from "something" to, for example, "nothing". How to accomplish such thing? Or maybe i should use something else as a container and not a vector?
011011010110000101100011011010000110100101101110 0110010101110011
As an aside, you don't need a typedef with every struct definition - you can use the 'tag' of the struct in the same way you can use a class name. Two options: 1. If you really only have an int that serves as soem sort of unique ID, and some value associated to that ID, then you should use a map rather than a vector. std::map automatically sorts your elements by your key values when you insert them. Retrieving an item is a simple and very fast operation. 2. If there's more to it, you can still use the std::find() method to locate a specific struct in your vector. You only need to define an equality operator for your struct like this: (not tested)
#include <vector> // std::vector
#include <algorithm> //std::find()
struct S {
int a;
std::string b;
bool operator==(const S& s) const {
return s.a==a;
}
S(int a_) : a(a_) {} // constructor, just for convenience
S(int a_, std::string b_) : a(a_), b(b_) {} // constructor, even more convenient
}int main() {
// ...
std::vector s;
s.push_back(S(1, "hi"));
// ...
s.push_back(S(10, "hello"));std::vector::iterator location = std::find(s.begin(), s.end(), 5); // finds element with a==5
std::string result;
if (location != s.end(); // found something?
result = location->b;
}You can check the online documentation on
std::find()
andstd::find_if()
for examples. P.S.: anyone knows what is messing with code coloring here? :confused:modified on Friday, April 8, 2011 2:40 AM
-
As an aside, you don't need a typedef with every struct definition - you can use the 'tag' of the struct in the same way you can use a class name. Two options: 1. If you really only have an int that serves as soem sort of unique ID, and some value associated to that ID, then you should use a map rather than a vector. std::map automatically sorts your elements by your key values when you insert them. Retrieving an item is a simple and very fast operation. 2. If there's more to it, you can still use the std::find() method to locate a specific struct in your vector. You only need to define an equality operator for your struct like this: (not tested)
#include <vector> // std::vector
#include <algorithm> //std::find()
struct S {
int a;
std::string b;
bool operator==(const S& s) const {
return s.a==a;
}
S(int a_) : a(a_) {} // constructor, just for convenience
S(int a_, std::string b_) : a(a_), b(b_) {} // constructor, even more convenient
}int main() {
// ...
std::vector s;
s.push_back(S(1, "hi"));
// ...
s.push_back(S(10, "hello"));std::vector::iterator location = std::find(s.begin(), s.end(), 5); // finds element with a==5
std::string result;
if (location != s.end(); // found something?
result = location->b;
}You can check the online documentation on
std::find()
andstd::find_if()
for examples. P.S.: anyone knows what is messing with code coloring here? :confused:modified on Friday, April 8, 2011 2:40 AM
Hey thanks! This gave me some ideas :) P.s. i know that i dont need typedefs for structs ;) btw. i had some time ago same issue with coloring thing - that is because you haven't probably replace html tags (<>)
011011010110000101100011011010000110100101101110 0110010101110011
-
Hey thanks! This gave me some ideas :) P.s. i know that i dont need typedefs for structs ;) btw. i had some time ago same issue with coloring thing - that is because you haven't probably replace html tags (<>)
011011010110000101100011011010000110100101101110 0110010101110011
Glad to be of help.
csrss wrote:
you haven't probably replace html tags
Good call! After replacing the < and > in the
#include
lines it worked. It's strange though, inside the actual code this wasn't neccesary.