Archived

This topic is now archived and is closed to further replies.

Zeblar Nagrim

const vector

Recommended Posts

How to initialize a const vector? I want to add 2 elements of my Rect class to a vector. Something like this (doesn't work):
const vector<Rect> rects(2) = vector<Rect>(Rect(0,0,333,333),Rect(0,0,333,333));
Please help! [edited by - Zeblar Nagrim on November 15, 2003 4:39:18 AM]

Share this post


Link to post
Share on other sites
Here is the lazy pachy solution (dono how else to do it, but I bet theres a better way):

vector<Rect> tmp;
tmp.push_back(Rect(0,0,333,333));
tmp.push_back(Rect(0,0,333,333));
const vector<Rect> rects=tmp;


"Stop trying to hit me and hit me!"

Share this post


Link to post
Share on other sites
quote:
Original post by emileej
Here is the lazy pachy solution (dono how else to do it, but I bet theres a better way):

vector<Rect> tmp;
tmp.push_back(Rect(0,0,333,333));
tmp.push_back(Rect(0,0,333,333));
const vector<Rect> rects=tmp;


"Stop trying to hit me and hit me!"


Thanks!

Share this post


Link to post
Share on other sites
How about:

#include <cstddef>
#include <vector>

int main() {
const int something_init[] = { 1, 2, 3 };
const std::size_t something_size = sizeof(something_init)/sizeof(*something_init);
std::vector <int> something(something_init, &something_init[something_size]);

// ...

}


A bit overly explicit there, just in case...



[edited by - Null and Void on November 15, 2003 4:58:42 AM]

Share this post


Link to post
Share on other sites