473,396 Members | 1,712 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Vector of Arrays in C++

Hi,
Im having lots of problems trying to implement a vector of arrays in C++.
Im trying to make a vector that will hold coordinates. Ive decided to
implement a coordinate as an array of integers of length 2.
My code to initialise this array is:
vector<int[2]> coordVector;
This seems to compile ok but im not sure if it is making a vector of arrays
because the command:
coordVector.push_back({0,0}) does not work.

Im sure im just doing something simple wrong but im having a nightmare
trying to fix it. Does anyone have any example code that uses a vector of
arrays that i could look at?

Thanks,
Vipa.
Jul 19 '05 #1
1 39256

claire.bell1 <cl**********@ntlworld.com> wrote in message
news:cl*******************@newsfep2-win.server.ntli.net...
Hi,
Im having lots of problems trying to implement a vector of arrays in C++.
I'm not surprised. Containers require that the elements they
contain be copyable and assignable. Arrays do not meet this
requirement. You're already using a container (vector), so
why are you using an array? How about a vector of vectors?
Im trying to make a vector that will hold coordinates. Ive decided to
implement a coordinate as an array of integers of length 2.
IMO a poor decision. Either create a 'coordinate' type (class),
or if you only need 'raw' storage for two values, use a 'std::pair'
My code to initialise this array is:
vector<int[2]> coordVector;
This seems to compile ok but im not sure if it is making a vector of arrays because the command:
coordVector.push_back({0,0}) does not work.
(If it were allowed) the element type is 'int[2]', so
'push_back()'s argument must be this type or a type
convertible to it. {0, 0} is not a valid expression
except as an array initializer. It certainly does
not have type 'int[2]'.

Im sure im just doing something simple wrong
Yes, you're trying to store noncopyable, nonassignable
items in a container.
but im having a nightmare
trying to fix it.
As long as you continue to try to break the language
rules, the nightmares will continue.
Does anyone have any example code that uses a vector of
arrays that i could look at?


Nope, because that's illegal.

Try:

#include <iostream>
#include <ostream>
#include <utility>
#include <vector>

typedef std::pair<int, int> coord_t;
typedef std::vector<coord_t> coord_list_t;

std::ostream& operator<<(std::ostream& os,
const coord_t& coord)
{
return os << coord.first << ", " << coord.second;
}

std::ostream& operator<<(std::ostream& os,
const coord_list_t& lst)
{
for(coord_list_t::size_type i = 0; i != lst.size(); ++i)
os << lst[i] << '\n';

return os;
}

int main()
{
coord_list_t coordVector;
coordVector.push_back(coord_t( 0, 0));
coordVector.push_back(coord_t(42, 25));
coordVector.push_back(coord_t(18, 69));

std::cout << coordVector << '\n';
return 0;
}
BTW which C++ book(s) are you reading? Perhaps you
need better ones.

-Mike

Jul 19 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: {AGUT2}=IWIK= | last post by:
Hello all, It's my fisrt post here and I am feeling a little stupid here, so go easy.. :) (Oh, and I've spent _hours_ searching...) I am desperately trying to read in an ASCII...
19
by: Carlo Milanesi | last post by:
Mathematically speaking, a 'vector' is something you can add to another vector and multiply by a number. But in C++, the following code is illegal: std::vector<double> v1(3), v2(3); v1 + v2; //...
4
by: Oliver Gebele | last post by:
/* OK, after years i'm still more into C; but i already do understand some C++. And there are still many things about the STL which i do not know... I try to put 8-character-arrays in a...
8
by: Hagen | last post by:
Hi, I have a question that you probably shouldn´t worry about since the compiler cares for it, but anyways: When you run your compiler with optimization turned on (eg. g++ with -Ox flag) and...
17
by: Havatcha | last post by:
Does anyone have a benchmark for the processing overhead of the STL Vector class, vs a C style array? I would dearly love to use Vectors, but am paranoid about slowing my real-time code down. Can...
13
by: Ben | last post by:
I have a program which is using a lot of memory. At the moment I store a lot of pointers to objects in std::vector. (millions of them) I have three questions: 1) Lets say the average Vector...
2
by: Priya Mishra | last post by:
Hi All It was very nice to intract with this group, While in my previous post, I was suggested to reffer the link, in order to learn C++, Well I was going thoruigh the link in which i had some...
12
by: mast2as | last post by:
Hi everyone I am working on some code that uses colors. Until recently this code used colors represented a tree floats (RGB format) but recently changed so colors are now defined as spectrum....
7
by: nw | last post by:
Hi, We've been having a discussion at work and I'm wondering if anyone here would care to offer an opinion or alternative solution. Aparently in the C programming HPC community it is common to...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.