std::vector<int[2]> intializing trouble
Author |
Message |
John Siedlick #1 / 15
|
 std::vector<int[2]> intializing trouble
I am trying to create a vector containing simple fixed sized base type arrays. I want to create an initial element in the vector that will be filled later. I do not have a concrete base object to construct from (or else I would simply call vector::push_back). I thought I could use vector::resize or vector::push_back as follows, but no luck. Can anyone explain why the second example does not work? This works: std::vector<int> test; test.resize(1); or test.push_back(std::vector<int>::value_type()); This does not work. std::vector<int[2]> test; test.resize(1); or test.push_back(std::vector<int[2]>::value_type()); Thanks for any input. John
|
Tue, 23 Aug 2005 00:33:14 GMT |
|
 |
Igor Tandetni #2 / 15
|
 std::vector<int[2]> intializing trouble
You can't do that - C arrays are not assignable. In other words, this doesn't work: int a[2]; int b[2]; b = a; // doesn't compile int c[2] = a; // doesn't compile Consider using vector<pair<int, int> > -- With best wishes, Igor Tandetnik "For every complex problem, there is a solution that is simple, neat, and wrong." H.L. Mencken
Quote: > I am trying to create a vector containing simple fixed > sized base type arrays. I want to create an initial > element in the vector that will be filled later. I do > not have a concrete base object to construct from (or > else I would simply call vector::push_back). > I thought I could use vector::resize or vector::push_back > as follows, but no luck. > Can anyone explain why the second example does not work? > This works: > std::vector<int> test; > test.resize(1); > or > test.push_back(std::vector<int>::value_type()); > This does not work. > std::vector<int[2]> test; > test.resize(1); > or > test.push_back(std::vector<int[2]>::value_type()); > Thanks for any input. > John
|
Tue, 23 Aug 2005 01:22:16 GMT |
|
 |
John #3 / 15
|
 std::vector<int[2]> intializing trouble
vector<pair<int, int> > won't work as I have a C array that is 928 elements. I only used [2] as a simple example.
Quote: > You can't do that - C arrays are not assignable. In other words, this > doesn't work: > int a[2]; > int b[2]; > b = a; // doesn't compile > int c[2] = a; // doesn't compile > Consider using vector<pair<int, int> > > -- > With best wishes, > Igor Tandetnik > "For every complex problem, there is a solution that is simple, neat, > and wrong." H.L. Mencken
> > I am trying to create a vector containing simple fixed > > sized base type arrays. I want to create an initial > > element in the vector that will be filled later. I do > > not have a concrete base object to construct from (or > > else I would simply call vector::push_back). > > I thought I could use vector::resize or vector::push_back > > as follows, but no luck. > > Can anyone explain why the second example does not work? > > This works: > > std::vector<int> test; > > test.resize(1); > > or > > test.push_back(std::vector<int>::value_type()); > > This does not work. > > std::vector<int[2]> test; > > test.resize(1); > > or > > test.push_back(std::vector<int[2]>::value_type()); > > Thanks for any input. > > John
|
Tue, 23 Aug 2005 01:33:21 GMT |
|
 |
Ken Alverso #4 / 15
|
 std::vector<int[2]> intializing trouble
Quote: > vector<pair<int, int> > won't work as I have a C array that is 928 elements. > I only used [2] as a simple example.
vector<vector<int> > is probably your best choice then. Ken
|
Tue, 23 Aug 2005 01:46:42 GMT |
|
 |
c nicholso #5 / 15
|
 std::vector<int[2]> intializing trouble
what's wrong with vector< pair< pair< pair< pair<pair< pair<pair< pair<pair< pair<pair< pair<pair< pair<pair< pair<pair< pair<pair< pair<pair< pair<pair< pair<pair< pair<pair< pair<pair< pair<pair< pair<pair< pair<pair< pair<pair< pair<pair< pair<pair< pair<pair< pair<pair< pair<pair< pair<pair< pair<pair< pair<pair< pair<pair< pair<pair< pair<pair< pair<pair< pair<pair< pair<pair< pair<pair< pair<pair< pair<pair< pair<pair< pair<pair< pair<pair< pair<pair< pair<pair< pair<pair< pair<pair< pair<pair< pair<pair< pair<pair< pair<pair< pair<pair< pair<pair< pair<pair< pair<pair< pair<pair< pair<pair< pair<pair< pair<pair< pair<pair< pair<pair< pair<pair< pair<? maybe you could simplify it with recursive macros! =] charles nicholson
Quote:
> > vector<pair<int, int> > won't work as I have a C array that is 928 > elements. > > I only used [2] as a simple example. > vector<vector<int> > is probably your best choice then. > Ken
|
Tue, 23 Aug 2005 03:41:45 GMT |
|
 |
Igor Tandetni #6 / 15
|
 std::vector<int[2]> intializing trouble
How would you access a k-th element of such a tuple, with k determined at run-time? -- With best wishes, Igor Tandetnik "For every complex problem, there is a solution that is simple, neat, and wrong." H.L. Mencken
Quote: > what's wrong with > vector< pair< pair< pair< pair<pair< pair<pair< pair<pair< pair<pair< > pair<pair< pair<pair< pair<pair< pair<pair< pair<pair< pair<pair< pair<pair< > pair<pair< pair<pair< pair<pair< pair<pair< pair<pair< pair<pair< pair<pair< > pair<pair< pair<pair< pair<pair< pair<pair< pair<pair< pair<pair< pair<pair< > pair<pair< pair<pair< pair<pair< pair<pair< pair<pair< pair<pair< pair<pair< > pair<pair< pair<pair< pair<pair< pair<pair< pair<pair< pair<pair< pair<pair< > pair<pair< pair<pair< pair<pair< pair<pair< pair<pair< pair<pair< pair<pair< > pair<pair< pair<pair< pair<pair< pair<pair< pair<pair< pair<pair< pair<pair< > pair<pair< pair<pair< pair<pair< pair<? > maybe you could simplify it with recursive macros! > =] > charles nicholson
> > > vector<pair<int, int> > won't work as I have a C array that is 928 > > elements. > > > I only used [2] as a simple example. > > vector<vector<int> > is probably your best choice then. > > Ken
|
Tue, 23 Aug 2005 03:50:48 GMT |
|
 |
c nicholso #7 / 15
|
 std::vector<int[2]> intializing trouble
Why, with more macros, of course! (it was a joke, btw) charles nicholson
Quote: > How would you access a k-th element of such a tuple, with k determined > at run-time? > -- > With best wishes, > Igor Tandetnik > "For every complex problem, there is a solution that is simple, neat, > and wrong." H.L. Mencken
> > what's wrong with > > vector< pair< pair< pair< pair<pair< pair<pair< pair<pair< pair<pair< > > pair<pair< pair<pair< pair<pair< pair<pair< pair<pair< pair<pair< > pair<pair< > > pair<pair< pair<pair< pair<pair< pair<pair< pair<pair< pair<pair< > pair<pair< > > pair<pair< pair<pair< pair<pair< pair<pair< pair<pair< pair<pair< > pair<pair< > > pair<pair< pair<pair< pair<pair< pair<pair< pair<pair< pair<pair< > pair<pair< > > pair<pair< pair<pair< pair<pair< pair<pair< pair<pair< pair<pair< > pair<pair< > > pair<pair< pair<pair< pair<pair< pair<pair< pair<pair< pair<pair< > pair<pair< > > pair<pair< pair<pair< pair<pair< pair<pair< pair<pair< pair<pair< > pair<pair< > > pair<pair< pair<pair< pair<pair< pair<? > > maybe you could simplify it with recursive macros! > > =] > > charles nicholson
> > > > vector<pair<int, int> > won't work as I have a C array that is 928 > > > elements. > > > > I only used [2] as a simple example. > > > vector<vector<int> > is probably your best choice then. > > > Ken
|
Tue, 23 Aug 2005 03:57:11 GMT |
|
 |
Ken Alverso #8 / 15
|
 std::vector<int[2]> intializing trouble
Probably something along the lines of (after changing the format from pair<pair<>,int> to pair<int,pair<> >): template <typename tuple> int& access_element(tuple& t, int n) { if (n == 0) return t.first; else return access_element(t.second,n-1); Quote: }
template <> int& access_element(int& t, int n) { if (n != 0) throw std::exception("accessing beyond the end of the tuple"); return t; Quote: }
pair<int,pair<int,pair<int,int> > > tuple; for (int i=0;i<4;++i) access_element(tuple,i) = 2*i; for (int i=0;i<4;++i) std::cerr << access_element(tuple,i) << std::endl; Not that I recommend such a thing :) Ken
Quote: > How would you access a k-th element of such a tuple, with k determined > at run-time? > -- > With best wishes, > Igor Tandetnik > "For every complex problem, there is a solution that is simple, neat, > and wrong." H.L. Mencken
> > what's wrong with > > vector< pair< pair< pair< pair<pair< pair<pair< pair<pair< pair<pair< > > pair<pair< pair<pair< pair<pair< pair<pair< pair<pair< pair<pair< > pair<pair< > > pair<pair< pair<pair< pair<pair< pair<pair< pair<pair< pair<pair< > pair<pair< > > pair<pair< pair<pair< pair<pair< pair<pair< pair<pair< pair<pair< > pair<pair< > > pair<pair< pair<pair< pair<pair< pair<pair< pair<pair< pair<pair< > pair<pair< > > pair<pair< pair<pair< pair<pair< pair<pair< pair<pair< pair<pair< > pair<pair< > > pair<pair< pair<pair< pair<pair< pair<pair< pair<pair< pair<pair< > pair<pair< > > pair<pair< pair<pair< pair<pair< pair<pair< pair<pair< pair<pair< > pair<pair< > > pair<pair< pair<pair< pair<pair< pair<? > > maybe you could simplify it with recursive macros! > > =] > > charles nicholson
> > > > vector<pair<int, int> > won't work as I have a C array that is 928 > > > elements. > > > > I only used [2] as a simple example. > > > vector<vector<int> > is probably your best choice then. > > > Ken
|
Tue, 23 Aug 2005 04:04:24 GMT |
|
 |
Igor Tandetni #9 / 15
|
 std::vector<int[2]> intializing trouble
That would work, though it makes accessing n-th element an operation of O(n) complexity instead of O(1). Also, recursive functions typically don't get inlined, so it's going to be slow and bloated. -- With best wishes, Igor Tandetnik "For every complex problem, there is a solution that is simple, neat, and wrong." H.L. Mencken
Quote: > Probably something along the lines of (after changing the format from > pair<pair<>,int> to pair<int,pair<> >): > template <typename tuple> > int& access_element(tuple& t, int n) { > if (n == 0) return t.first; else return
access_element(t.second,n-1); Quote: > } > template <> > int& access_element(int& t, int n) { > if (n != 0) throw std::exception("accessing beyond the end of the > tuple"); > return t; > } > pair<int,pair<int,pair<int,int> > > tuple; > for (int i=0;i<4;++i) access_element(tuple,i) = 2*i; > for (int i=0;i<4;++i) std::cerr << access_element(tuple,i) << std::endl; > Not that I recommend such a thing :) > Ken
> > How would you access a k-th element of such a tuple, with k determined > > at run-time? > > -- > > With best wishes, > > Igor Tandetnik > > "For every complex problem, there is a solution that is simple, neat, > > and wrong." H.L. Mencken
> > > what's wrong with > > > vector< pair< pair< pair< pair<pair< pair<pair< pair<pair< > pair<pair< > > > pair<pair< pair<pair< pair<pair< pair<pair< pair<pair< pair<pair< > > pair<pair< > > > pair<pair< pair<pair< pair<pair< pair<pair< pair<pair< pair<pair< > > pair<pair< > > > pair<pair< pair<pair< pair<pair< pair<pair< pair<pair< pair<pair< > > pair<pair< > > > pair<pair< pair<pair< pair<pair< pair<pair< pair<pair< pair<pair< > > pair<pair< > > > pair<pair< pair<pair< pair<pair< pair<pair< pair<pair< pair<pair< > > pair<pair< > > > pair<pair< pair<pair< pair<pair< pair<pair< pair<pair< pair<pair< > > pair<pair< > > > pair<pair< pair<pair< pair<pair< pair<pair< pair<pair< pair<pair< > > pair<pair< > > > pair<pair< pair<pair< pair<pair< pair<? > > > maybe you could simplify it with recursive macros! > > > =] > > > charles nicholson
> > > > > vector<pair<int, int> > won't work as I have a C array that is > 928 > > > > elements. > > > > > I only used [2] as a simple example. > > > > vector<vector<int> > is probably your best choice then. > > > > Ken
|
Tue, 23 Aug 2005 04:11:37 GMT |
|
 |
Ken Alverso #10 / 15
|
 std::vector<int[2]> intializing trouble
Actually, it's not technically recursive, since each call is to a different instance of the function. It will create N functions, where N is the size of your tuple, but I wouldn't be surprised if the compiler was able to completely inline the access. (I wouldn't be surprised if it didn't, either.) Ken
Quote: > That would work, though it makes accessing n-th element an operation of > O(n) complexity instead of O(1). Also, recursive functions typically > don't get inlined, so it's going to be slow and bloated. > -- > With best wishes, > Igor Tandetnik > "For every complex problem, there is a solution that is simple, neat, > and wrong." H.L. Mencken
> > Probably something along the lines of (after changing the format from > > pair<pair<>,int> to pair<int,pair<> >): > > template <typename tuple> > > int& access_element(tuple& t, int n) { > > if (n == 0) return t.first; else return > access_element(t.second,n-1); > > } > > template <> > > int& access_element(int& t, int n) { > > if (n != 0) throw std::exception("accessing beyond the end of the > > tuple"); > > return t; > > } > > pair<int,pair<int,pair<int,int> > > tuple; > > for (int i=0;i<4;++i) access_element(tuple,i) = 2*i; > > for (int i=0;i<4;++i) std::cerr << access_element(tuple,i) << > std::endl; > > Not that I recommend such a thing :) > > Ken
> > > How would you access a k-th element of such a tuple, with k > determined > > > at run-time? > > > -- > > > With best wishes, > > > Igor Tandetnik > > > "For every complex problem, there is a solution that is simple, > neat, > > > and wrong." H.L. Mencken
> > > > what's wrong with > > > > vector< pair< pair< pair< pair<pair< pair<pair< pair<pair< > > pair<pair< > > > > pair<pair< pair<pair< pair<pair< pair<pair< pair<pair< pair<pair< > > > pair<pair< > > > > pair<pair< pair<pair< pair<pair< pair<pair< pair<pair< pair<pair< > > > pair<pair< > > > > pair<pair< pair<pair< pair<pair< pair<pair< pair<pair< pair<pair< > > > pair<pair< > > > > pair<pair< pair<pair< pair<pair< pair<pair< pair<pair< pair<pair< > > > pair<pair< > > > > pair<pair< pair<pair< pair<pair< pair<pair< pair<pair< pair<pair< > > > pair<pair< > > > > pair<pair< pair<pair< pair<pair< pair<pair< pair<pair< pair<pair< > > > pair<pair< > > > > pair<pair< pair<pair< pair<pair< pair<pair< pair<pair< pair<pair< > > > pair<pair< > > > > pair<pair< pair<pair< pair<pair< pair<? > > > > maybe you could simplify it with recursive macros! > > > > =] > > > > charles nicholson
> > > > > > vector<pair<int, int> > won't work as I have a C array that is > > 928 > > > > > elements. > > > > > > I only used [2] as a simple example. > > > > > vector<vector<int> > is probably your best choice then. > > > > > Ken
|
Tue, 23 Aug 2005 04:16:51 GMT |
|
 |
Ken Alverso #11 / 15
|
 std::vector<int[2]> intializing trouble
Err...that can't work with a runtime value though. Right. Empirical testing shows that the element is accessed directly when the index is a constant integer and the function is inlined. Taking a dynamic index does take O(N) time, but the entire process is inlined (at least at a size 6 tuple). Ken
Quote: > Actually, it's not technically recursive, since each call is to a > different instance of the function. It will create N functions, where N > is the size of your tuple, but I wouldn't be surprised if the compiler > was able to completely inline the access. (I wouldn't be surprised if > it didn't, either.) > Ken
> > That would work, though it makes accessing n-th element an operation > of > > O(n) complexity instead of O(1). Also, recursive functions typically > > don't get inlined, so it's going to be slow and bloated. > > -- > > With best wishes, > > Igor Tandetnik > > "For every complex problem, there is a solution that is simple, neat, > > and wrong." H.L. Mencken
> > > Probably something along the lines of (after changing the format > from > > > pair<pair<>,int> to pair<int,pair<> >): > > > template <typename tuple> > > > int& access_element(tuple& t, int n) { > > > if (n == 0) return t.first; else return > > access_element(t.second,n-1); > > > } > > > template <> > > > int& access_element(int& t, int n) { > > > if (n != 0) throw std::exception("accessing beyond the end of the > > > tuple"); > > > return t; > > > } > > > pair<int,pair<int,pair<int,int> > > tuple; > > > for (int i=0;i<4;++i) access_element(tuple,i) = 2*i; > > > for (int i=0;i<4;++i) std::cerr << access_element(tuple,i) << > > std::endl; > > > Not that I recommend such a thing :) > > > Ken
> > > > How would you access a k-th element of such a tuple, with k > > determined > > > > at run-time? > > > > -- > > > > With best wishes, > > > > Igor Tandetnik > > > > "For every complex problem, there is a solution that is simple, > > neat, > > > > and wrong." H.L. Mencken
> > > > > what's wrong with > > > > > vector< pair< pair< pair< pair<pair< pair<pair< pair<pair< > > > pair<pair< > > > > > pair<pair< pair<pair< pair<pair< pair<pair< pair<pair< > pair<pair< > > > > pair<pair< > > > > > pair<pair< pair<pair< pair<pair< pair<pair< pair<pair< > pair<pair< > > > > pair<pair< > > > > > pair<pair< pair<pair< pair<pair< pair<pair< pair<pair< > pair<pair< > > > > pair<pair< > > > > > pair<pair< pair<pair< pair<pair< pair<pair< pair<pair< > pair<pair< > > > > pair<pair< > > > > > pair<pair< pair<pair< pair<pair< pair<pair< pair<pair< > pair<pair< > > > > pair<pair< > > > > > pair<pair< pair<pair< pair<pair< pair<pair< pair<pair< > pair<pair< > > > > pair<pair< > > > > > pair<pair< pair<pair< pair<pair< pair<pair< pair<pair< > pair<pair< > > > > pair<pair< > > > > > pair<pair< pair<pair< pair<pair< pair<? > > > > > maybe you could simplify it with recursive macros! > > > > > =] > > > > > charles nicholson
> > > > > > > vector<pair<int, int> > won't work as I have a C array that > is > > > 928 > > > > > > elements. > > > > > > > I only used [2] as a simple example. > > > > > > vector<vector<int> > is probably your best choice then. > > > > > > Ken
|
Tue, 23 Aug 2005 04:39:14 GMT |
|
 |
Ken Alverso #12 / 15
|
 std::vector<int[2]> intializing trouble
Quote: > That would work, though it makes accessing n-th element an operation of > O(n) complexity instead of O(1). Also, recursive functions typically > don't get inlined, so it's going to be slow and bloated.
Of course, you can get O(lg(n)) performance out of a pair<pair<>,pair<> Quote: > structure.
template <typename tuple> inline int size(const tuple& t) { return size(t.first)+size(t.second); Quote: }
template <> inline int size(const int& t) { return 1; Quote: }
template <typename tuple> inline int& get(tuple& t, int i) { if (i < size(t.first)) return get(t.first,i); else return get(t.second,i-size(t.first)); Quote: }
template <> inline int& get(int& t, int i) { return t; Quote: }
pair<pair<pair<int,int>,pair<int,int> >,pair<pair<int,int>,pair<int,int> Quote: > > tuple;
for (int i=0;i<8;++i) get(tuple,i) = 2*i; for (int i=0;i<8;++i) std::cerr << get(tuple,i) << std::endl; Again, not that I recommend any of this hackery :) Ok, back to productive work with me. Ken
|
Tue, 23 Aug 2005 04:55:48 GMT |
|
 |
c nicholso #13 / 15
|
 std::vector<int[2]> intializing trouble
Quote: > Err...that can't work with a runtime value though. Right.
Right, until you make a lisp-ish cascading macro like Loki's type-list creators that will access the Nth element of any composed pair construct... too gross to think about.... charles nicholson Quote: > Empirical testing shows that the element is accessed directly when the > index is a constant integer and the function is inlined. > Taking a dynamic index does take O(N) time, but the entire process is > inlined (at least at a size 6 tuple). > Ken
> > Actually, it's not technically recursive, since each call is to a > > different instance of the function. It will create N functions, where > N > > is the size of your tuple, but I wouldn't be surprised if the compiler > > was able to completely inline the access. (I wouldn't be surprised if > > it didn't, either.) > > Ken
> > > That would work, though it makes accessing n-th element an operation > > of > > > O(n) complexity instead of O(1). Also, recursive functions typically > > > don't get inlined, so it's going to be slow and bloated. > > > -- > > > With best wishes, > > > Igor Tandetnik > > > "For every complex problem, there is a solution that is simple, > neat, > > > and wrong." H.L. Mencken
> > > > Probably something along the lines of (after changing the format > > from > > > > pair<pair<>,int> to pair<int,pair<> >): > > > > template <typename tuple> > > > > int& access_element(tuple& t, int n) { > > > > if (n == 0) return t.first; else return > > > access_element(t.second,n-1); > > > > } > > > > template <> > > > > int& access_element(int& t, int n) { > > > > if (n != 0) throw std::exception("accessing beyond the end of > the > > > > tuple"); > > > > return t; > > > > } > > > > pair<int,pair<int,pair<int,int> > > tuple; > > > > for (int i=0;i<4;++i) access_element(tuple,i) = 2*i; > > > > for (int i=0;i<4;++i) std::cerr << access_element(tuple,i) << > > > std::endl; > > > > Not that I recommend such a thing :) > > > > Ken
> > > > > How would you access a k-th element of such a tuple, with k > > > determined > > > > > at run-time? > > > > > -- > > > > > With best wishes, > > > > > Igor Tandetnik > > > > > "For every complex problem, there is a solution that is simple, > > > neat, > > > > > and wrong." H.L. Mencken
> > > > > > what's wrong with > > > > > > vector< pair< pair< pair< pair<pair< pair<pair< pair<pair< > > > > pair<pair< > > > > > > pair<pair< pair<pair< pair<pair< pair<pair< pair<pair< > > pair<pair< > > > > > pair<pair< > > > > > > pair<pair< pair<pair< pair<pair< pair<pair< pair<pair< > > pair<pair< > > > > > pair<pair< > > > > > > pair<pair< pair<pair< pair<pair< pair<pair< pair<pair< > > pair<pair< > > > > > pair<pair< > > > > > > pair<pair< pair<pair< pair<pair< pair<pair< pair<pair< > > pair<pair< > > > > > pair<pair< > > > > > > pair<pair< pair<pair< pair<pair< pair<pair< pair<pair< > > pair<pair< > > > > > pair<pair< > > > > > > pair<pair< pair<pair< pair<pair< pair<pair< pair<pair< > > pair<pair< > > > > > pair<pair< > > > > > > pair<pair< pair<pair< pair<pair< pair<pair< pair<pair< > > pair<pair< > > > > > pair<pair< > > > > > > pair<pair< pair<pair< pair<pair< pair<? > > > > > > maybe you could simplify it with recursive macros! > > > > > > =] > > > > > > charles nicholson
> > > > > > > > vector<pair<int, int> > won't work as I have a C array > that > > is > > > > 928 > > > > > > > elements. > > > > > > > > I only used [2] as a simple example. > > > > > > > vector<vector<int> > is probably your best choice then. > > > > > > > Ken
|
Tue, 23 Aug 2005 05:18:14 GMT |
|
 |
Ken Alverso #14 / 15
|
 std::vector<int[2]> intializing trouble
Quote:
> > Err...that can't work with a runtime value though. Right. > Right, until you make a lisp-ish cascading macro like Loki's type-list > creators that will access the Nth element of any composed pair construct... > too gross to think about....
Actually, that's more or less what I did. I meant it can't work in constant time with a runtime index. It works just fine with a runtime index in linear time. Or a compile time index in constant time. I imagine Loki's access speed is similar. Ken
|
Tue, 23 Aug 2005 05:30:19 GMT |
|
 |
Igor Tandetni #15 / 15
|
 std::vector<int[2]> intializing trouble
Quote: > It works just fine with a runtime index in linear time. Or a compile > time index in constant time. I imagine Loki's access speed is
similar. Loki works on lists of types, not on lists of values. Type manipulation is always done compile-time. So, it makes compile time linear or worse, but no run-time code is actually generated. -- With best wishes, Igor Tandetnik "For every complex problem, there is a solution that is simple, neat, and wrong." H.L. Mencken
|
Tue, 23 Aug 2005 05:48:37 GMT |
|
|
|