473,385 Members | 1,829 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,385 software developers and data experts.

map to vector conversion inside class

I have a class which looks like this

template< typename K, typename I >
Class X {
typedef typename map<K,I> Tcontainer1;
typedef typename vector<K> Tcontainer2;

Tcontainer tmap;

public:
typedef typename Tcontainer1::iterator item;
typedef typename Tcontainer1::const_iterator citem;

public:
const K& key(item it);
void container_change(void);
....

}

What I would like to do is dynamically change the container
from the map to the vector when container_change() is initiated.
This means that I would have to change types at runtime! Any
idea how to do this? The problem is that the class is heavily
used and I would not like to change the interface of the class
if possible. Anything I could change internally to achieve this?

Thanks,
--j

Jul 23 '05 #1
8 1801
John wrote:
I have a class which looks like this

template< typename K, typename I >
Class X {
typedef typename map<K,I> Tcontainer1;
typedef typename vector<K> Tcontainer2;

Tcontainer tmap;

public:
typedef typename Tcontainer1::iterator item;
typedef typename Tcontainer1::const_iterator citem;

public:
const K& key(item it);
void container_change(void);
...

}

What I would like to do is dynamically change the container
from the map to the vector when container_change() is initiated.
This means that I would have to change types at runtime! Any
idea how to do this? The problem is that the class is heavily
used and I would not like to change the interface of the class
if possible. Anything I could change internally to achieve this?


Have two pointers, one to a 'map<>' and the other to a 'vector<>',
and keep one of them null to indicate that the other one is used.

The rest of the types (like the iterators and so on) are not so easy
to maintain, but 'key' function will work, you just need to branch
inside it depending on what container is "active".

V
Jul 23 '05 #2

What about the returned item types?
Dynamically I know what type to return.

Is this possible to do?
const K& key(tmap::iterator it);

Jul 23 '05 #3
John wrote:
What about the returned item types?
Dynamically I know what type to return.

Is this possible to do?
const K& key(tmap::iterator it);


But isn't 'K' a template argument? You shouldn't have any problem
there, no? I am not sure what the 'iterator' here is for.
Jul 23 '05 #4

According to your suggestion

I have a class which would look like

template< typename K, typename I >
Class X {

typedef typename map<K,I> Tcontainer1;
typedef typename vector<K> Tcontainer2;

void *tmap;

public:
typedef typename Tcontainer1::iterator item;
typedef typename Tcontainer1::const_iterator citem;

public:
const K& key( tmap->iterator it);
void container_change(void);
....

}

But will this work? Even if tmap points to the vector or map depending
upon what it is pointing to? Can this be made to work?

Jul 23 '05 #5
John wrote:
According to your suggestion

I have a class which would look like

template< typename K, typename I >
Class X {
That's not even going to compile!

typedef typename map<K,I> Tcontainer1;
typedef typename vector<K> Tcontainer2;

void *tmap;

public:
typedef typename Tcontainer1::iterator item;
typedef typename Tcontainer1::const_iterator citem;

public:
const K& key( tmap->iterator it);
void container_change(void);
...

}

But will this work?
No.
Even if tmap points to the vector or map depending
upon what it is pointing to? Can this be made to work?


No, that's not what I suggested. Have two pointers, only one should
be "active", the non-null one:

template< typename K, typename I >
class X {
typedef std::map<K,I> Tcontainer1;
typedef std::vector<K> Tcontainer2;

Tcontainer1 *tmap; // one of these two pointers
Tcontainer2 *tvector; // is going to be null.

public:

X(); // should make one of the "containers" active

const K& key( ??? ); // think of changing the argument

void container_change(); // Change containers, how?
// No argument is supplied
// Do you just swap one with
// the other? I suppose it's OK...
};

You need to think more about it, I am guessing.

V
Jul 23 '05 #6
Cool

template< typename K, typename I >
class X {
typedef std::map<K,I> Tcontainer1;
typedef std::vector<K> Tcontainer2;

Tcontainer1 *tmap; // one of these two pointers
Tcontainer2 *tvector; // is going to be null.
bool container_is_map = true;

public:

X(); // should make one of the "containers" active

template< typename item>
const K& key( item x ); // think of changing the argument

void container_change(); // Change containers, how?
// No argument is supplied
// Do you just swap one with
// the other? I suppose it's OK...

};

Maybe something like this?

Jul 23 '05 #7
John wrote:
Cool

template< typename K, typename I >
class X {
typedef std::map<K,I> Tcontainer1;
typedef std::vector<K> Tcontainer2;

Tcontainer1 *tmap; // one of these two pointers
Tcontainer2 *tvector; // is going to be null.
bool container_is_map = true;

public:

X(); // should make one of the "containers" active

template< typename item>
const K& key( item x ); // think of changing the argument

void container_change(); // Change containers, how?
// No argument is supplied
// Do you just swap one with
// the other? I suppose it's OK...

};

Maybe something like this?


Sure. You can even specialise the 'key' member template on map::iterator
and vector::iterator if you want.

V
Jul 23 '05 #8


That's a cool idea,

Thanks,
--j

Jul 23 '05 #9

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

Similar topics

6
by: Matthias | last post by:
Hi, say I have a vector v1: std::vector<SomeType> v1; and I need a vector v2 of pointers to v1's elements: std::vector<SomeType*> v2;
17
by: Michael Hopkins | last post by:
Hi all I want to create a std::vector that goes from 1 to n instead of 0 to n-1. The only change this will have is in loops and when the vector returns positions of elements etc. I am calling...
11
by: sw | last post by:
Hi, Is it possible to insert a class <vec> which has a vector<double> member, into the vector<vec> veclist for e.g? I've been getting compilation errors when trying to insert using the vector...
12
by: ypjofficial | last post by:
Hello all, I have encountered with following strange problem. I am coding in C++ and using VC++ 6 compiler. I have a class strvector containing char * cstr as a private member and i have...
0
by: A Taylor | last post by:
Hello, I am getting the following error using .NET 2003 and I wonder if anyone can help me understand what is going on. godcDoc.cpp(228) : warning C4267: '=' : conversion from 'size_t' to...
24
by: toton | last post by:
Hi, I want to have a vector like class with some additional functionality (cosmetic one). So can I inherit a vector class to add the addition function like, CorresVector : public...
6
by: Bobrick | last post by:
Hi. Thanks to everyone who replied to my last post, it turns out it wasn't the line where I was trying to treat the variable in question as an array which was the problem, but the line above. ...
4
by: Josefo | last post by:
Hello, is someone so kind to tell me why I am getting the following errors ? vector_static_function.c:20: error: expected constructor, destructor, or type conversion before '.' token...
4
by: kungfuelmosan | last post by:
Hey guys, Im just getting into c++ at the moment so please bare with me Basically i need to declare a vector<stringstringArray(50) inside a class, however by doing so i am getting the following...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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?
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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,...

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.