473,657 Members | 2,572 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Is it possible to create a vector of vector?


Newbie alert: I come from the C side, struggling to learn C++.

I need a two-dimensional data structure. I first tried a regular
vector and added the 2nd dimension with my own structs and pointers. I
couldn't make it work. Too many illegal assignments.

My next attempt was to create a vector of vector (or list of vector,
etc.). The compiler (MSVC++) doesn't seem to like such structure. I
guess I could try gcc, but let's hear the experts' opinions.

Your suggestions are most welcome.

-RFH

Jun 27 '08 #1
19 1517
LR
Ramon F Herrera wrote:
My next attempt was to create a vector of vector (or list of vector,
etc.).
This seems a very reasonable approach.

The compiler (MSVC++) doesn't seem to like such structure.
Can you please post a small example that demonstrates the problem.

Can you also be more specific about the problem that you're having.
LR
Jun 27 '08 #2
On 2 jun, 23:38, Ramon F Herrera <ra...@conexus. netwrote:
Newbie alert: I come from the C side, struggling to learn C++.

I need a two-dimensional data structure. I first tried a regular
vector and added the 2nd dimension with my own structs and pointers. I
couldn't make it work. Too many illegal assignments.

My next attempt was to create a vector of vector (or list of vector,
etc.). The compiler (MSVC++) doesn't seem to like such structure. I
guess I could try gcc, but let's hear the experts' opinions.

Your suggestions are most welcome.

-RFH
Mi english is not so good. Im trying to explain.

For example:

std::vector< std::vector<int myVector; // this creates a vector of
vector of integers

Now, you can do this:

std::vector<int vector;
vector.push_bac k(1);

myVector.push_b ack(vector);

then:

std::cout << myVector[0][0] << std::endl; // this prints 1

If you want to initialize a vector of vector with a fixed size use the
following line:

std::vector< std::vector<int myVector (number_of_rows ,
std::vector<int >(number_of_col ums, default_value)) ;

Read http://www.sgi.com/tech/stl/Vector.html to understand the
constructor calls.

I hope you understand.

Regards
--
Mauro Ciancio
Jun 27 '08 #3
Ramon F Herrera wrote:
Newbie alert: I come from the C side, struggling to learn C++.

I need a two-dimensional data structure. I first tried a regular
vector and added the 2nd dimension with my own structs and pointers. I
couldn't make it work. Too many illegal assignments.

My next attempt was to create a vector of vector (or list of vector,
etc.). The compiler (MSVC++) doesn't seem to like such structure. I
guess I could try gcc, but let's hear the experts' opinions.

Your suggestions are most welcome.

-RFH
#include <vector>

int main(int argc, char *argv[]) {
std::vector<std ::vector<int vectovect;
vectovect.resiz e(20);
for (std::vector<st d::vector<int:: iterator i = vectovect.begin ();
i != vectovect.end() ; ++i) {
i->resize(30);
}
}

--
Daniel Pitts' Tech Blog: <http://virtualinfinity .net/wordpress/>
Jun 27 '08 #4
Daniel Pitts wrote:
Ramon F Herrera wrote:
>Newbie alert: I come from the C side, struggling to learn C++.

I need a two-dimensional data structure. I first tried a regular
vector and added the 2nd dimension with my own structs and pointers. I
couldn't make it work. Too many illegal assignments.

My next attempt was to create a vector of vector (or list of vector,
etc.). The compiler (MSVC++) doesn't seem to like such structure. I
guess I could try gcc, but let's hear the experts' opinions.

Your suggestions are most welcome.

-RFH

#include <vector>

int main(int argc, char *argv[]) {
std::vector<std ::vector<int vectovect;
vectovect.resiz e(20);
for (std::vector<st d::vector<int:: iterator i = vectovect.begin ();
i != vectovect.end() ; ++i) {
i->resize(30);
}
}
Ah, listen to Mauro's advice. I didn't know about those constructors
that would ease your use :-)

--
Daniel Pitts' Tech Blog: <http://virtualinfinity .net/wordpress/>
Jun 27 '08 #5
On Jun 2, 11:50 pm, LR <lr...@superlin k.netwrote:
Ramon F Herrera wrote:
My next attempt was to create a vector of vector (or list of vector,
etc.).

This seems a very reasonable approach.
The compiler (MSVC++) doesn't seem to like such structure.

Can you please post a small example that demonstrates the problem.

Well, I figured out that it is possible to create such contraptions.
There are at least two ways:

typedef vector<RadioBut tonNode> RadioButtonGrou p;
vector<RadioBut tonGroup> ListOfRadioButt on;

and the more succinct:

vector<vector<R adioButtonNode> ListOfRadioButt on;

Now I have to investigate the kind suggestions from Mauro and Daniel.

Thanks, Daniel!
Grazie tanti, Mauro!

-Ramon


Jun 27 '08 #6
Ramon F Herrera wrote:
On Jun 2, 11:50 pm, LR <lr...@superlin k.netwrote:
>Ramon F Herrera wrote:
>>My next attempt was to create a vector of vector (or list of vector,
etc.).
This seems a very reasonable approach.
>>The compiler (MSVC++) doesn't seem to like such structure.
Can you please post a small example that demonstrates the problem.


Well, I figured out that it is possible to create such contraptions.
There are at least two ways:

typedef vector<RadioBut tonNode> RadioButtonGrou p;
vector<RadioBut tonGroup> ListOfRadioButt on;

and the more succinct:

vector<vector<R adioButtonNode> ListOfRadioButt on;
Your problem is here. The compiler is interpreting the closing >as a
single token. Put a space between them:

vector<vector<R adioButtonNode ListOfRadioButt on;

Note the space.

>
Now I have to investigate the kind suggestions from Mauro and Daniel.

Thanks, Daniel!
Grazie tanti, Mauro!

-Ramon

Jun 27 '08 #7
William Xu <wi*********@gm ail.comwrites:
>red floyd <no**********@e xample.comwrite s:
>Note the space.
>Which is really annoying...
Don't worry - I think it's fixed in the next release.
Jun 27 '08 #8
Ramon F Herrera <ra***@conexus. netwrote:
Newbie alert: I come from the C side, struggling to learn C++.

I need a two-dimensional data structure. I first tried a regular
vector and added the 2nd dimension with my own structs and pointers. I
couldn't make it work. Too many illegal assignments.

My next attempt was to create a vector of vector (or list of vector,
etc.). The compiler (MSVC++) doesn't seem to like such structure. I
guess I could try gcc, but let's hear the experts' opinions.

Your suggestions are most welcome.
Don't make a vector of vectors. Better would be to make a matrix class
that is inherently 2D. See the FAQ for an example.
Jun 27 '08 #9
Daniel T. wrote:
Ramon F Herrera <ra***@conexus. netwrote:
>Newbie alert: I come from the C side, struggling to learn C++.

I need a two-dimensional data structure. I first tried a regular
vector and added the 2nd dimension with my own structs and pointers. I
couldn't make it work. Too many illegal assignments.

My next attempt was to create a vector of vector (or list of vector,
etc.). The compiler (MSVC++) doesn't seem to like such structure. I
guess I could try gcc, but let's hear the experts' opinions.

Your suggestions are most welcome.

Don't make a vector of vectors. Better would be to make a matrix class
that is inherently 2D. See the FAQ for an example.
That depends very much on whether the 2D structure is rectangular or whether
it is ragged like a text broken up into lines of different length.

For a rectangular array, a matrix like container class is probably the way
to go as it guarantees the invariants for rectangularity. For a ragged
structure, I don't see what is wrong with a vector of vectors. Of course,
one can always encapsulate that in a class to hide implementation details.
But whether that is worth the effort cannot be said without knowing the
particulars of the project. (And, for all we know, the OP might be asking
about how to implement the internals of a text buffer class.)
Best

Kai-Uwe Bux
Jun 27 '08 #10

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

Similar topics

2
2403
by: Dave | last post by:
I'm crossposting this to both comp.lang.c++ and gnu.gcc because I'm not sure if this is correct behavior or not, and I'm using the gcc STL and compiler. When calling vector<int>::push_back(0), an iterator that I've set in a loop gets changed. Here's an example of the problem (sorry about the lack of indentation, posting this from Google): #include <vector> #include <iostream>
14
2657
by: LumisROB | last post by:
Is it possible to create matrixes with vector <vector <double >> ? If it is possible which is the element m23 ? You excuse but I am not an expert Thanks ROB
3
1595
by: Raider | last post by:
I'm trying to create a base class who will contain generic algoritms and derived class(es) who will perform problem-specific things. I'm unable to use dynamic polymorphism, because base class know nothing about problem-specific types. I wrote the following code using static polymorphism, but compiler do not allow me to use type traits as a template parameter, the following code not compiles: template <class CT> struct InfoClass {
23
7396
by: sandy | last post by:
I need (okay, I want) to make a dynamic array of my class 'Directory', within my class Directory (Can you already smell disaster?) Each Directory can have subdirectories so I thought to put these in an array. The application compiles but aborts without giving me any useful information. What I suspect is happening is infinite recursion. Each Directory object creates an array of Subdirectories each of which has an array of...
13
10227
by: ragged_hippy | last post by:
Hi, I wanted to create a vector of const references. Something like this: vector<const x&y; where x is a class name. Is this a valid statement? Will this work if I use a reference of 'y' somewhere else?
9
2187
by: Jazi | last post by:
Hello everyone, I was wondering if someone can help me figuring out some errors I keep getting when I try to create a dll library from existing c++ code using VS2005. The issue is related to a class that returns a vector of objects. There is no errors when I compile this in c++ "linux" and it works find but the issue is with the VS2005. Here is what I have: I have two classes: student class namespace StudentClass{ ...
8
5659
by: jonpb | last post by:
Hi, Is it possible to define a implicit operator from base to derived types in C#? I have a Point class and would like to derive a Vector class from it and add a couple new vector related functions, like Normalize(). The problem is Vector cannot use any of the Point operator overloads with a compile time error that there is no implicit conversion from Point to Vector. I tried adding an implicit operator to Vector: public static implicit...
7
2153
by: Rob | last post by:
This actually compiles and works but it doesn't seem like the best code, so I was wondering is there another way to do this? template <typename Tvector<T>* addDepth(T) { return new vector<T>; } ....a templated meth...
2
1100
by: Kurda Yon | last post by:
Hi, I just started to learn Python. I understood how one can create a class and define a method related with that class. According to my understanding every call of a methods is related with a specific object. For example, we have a method "length", than we call this method as the following "x.length()" or "y.length()" or "z.length()", where z, y, and z are objects of the class. I am wandering if it is possible to create a method...
0
8425
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8743
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8522
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8622
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6177
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5647
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
2745
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1973
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1736
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.