It sounds like a vector would be faster since you can keep it sorted and use a binary search for finding things.
It sounds like a vector would be faster since you can keep it sorted and use a binary search for finding things.
p.s. What the alphabet would look like without q and r.
Lucky, the vector code could probably be made faster if this:
Was changed to this:Code:VSETTINGS vset; for (int i = 0; i < items; ++i) { vset.name.push_back("ABCDEFG"); vset.value.push_back("HIJKLMNOP"); }
This could change your timings, although I don't know by how much. Reserving that space initially would mean no reallocations of the vector memory. I haven't tested it but it seems like it should be faster. As has been said already, it all depends on how well you write your code.Code:VSETTINGS vset; vset.name.reserve(items); // Reserve space in one fell swoop vset.value.reserve(items); // Reserve space in one fell swoop for (int i = 0; i < items; ++i) { vset.name[i] = "ABCDEFG"; vset.value[i] = "HIJKLMNOP"; }
"Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
-Christopher Hitchens
Thanks for pointing that out, however, that would negate the purpose of the application. As you said it could change the timings which is why it would throw it off. The purpose of the code was to compare the time it would take to insert x number of items and to delete them. The way I figure it, a given user won't normally know the number of items to be inserted into a vector/list.Originally posted by hk_mp5kpdw
Lucky, the vector code could probably be made faster...
This could change your timings...
A few more points. Reserve grabs the space, but does not call any ctor's thus you still need to use push_back(). You could use resize() and []. structs can have =, ctor's, copy ctor's, and dtors just like classes, and if they are not provided, default ones are generated, just like classes. If all the members of a struct have working ctor's,=,etc.. then the default ones will work fine, just like classes. The only difference between a struct and a class is that a struct is born with no class and let's just anyone play with it's privates.
Hahahaha... Nice.Originally posted by grib
The only difference between a struct and a class is that a struct is born with no class and let's just anyone play with it's privates.
As molestingly as grib put it, it's true that the only difference between a struct and class (depending on the compiler) is the default member type.
"play with it's privates" hehehe
so structs can have functions? or did i completely misunderstand that? lol
That would take about two seconds to make.Originally posted by XSquared
Because the vector class requires that the datatype have a defined assignment operator, which is not provided with structs.
Yep, structs can have functions (in C++ at least). In C, it is a different story.Originally posted by the Wookie
so structs can have functions? or did i completely misunderstand that? lol
oh ok i gotcha, but no constructor/destructor right?Originally posted by golfinguy4
Yep, structs can have functions (in C++ at least). In C, it is a different story.
structs are classes with a default access of public, that is the only difference.
p.s. What the alphabet would look like without q and r.
Originally posted by the Wookie
so structs can have functions? or did i completely misunderstand that? lolOriginally, structs were basically just groups of data combined with a common name. Eventually, compiler designers figured they were just so similar to classes that they are now handled identically, with the exception that the default member type of a struct is public and that of a class is private. Aside from that, everything you can do with a class you can also do with a struct.Originally posted by me
the only difference between a struct and class (depending on the compiler) is the default member type.