Hi,
i want to take a look at STL Associative Container, map.
but i can't find in google. Maybe i use the term to search.
stop by here, and ask for help.
actually, i have to create associative array template class similiary to map<string, int> container.
That's why i wanna take a look at the map template class code.
can anyone show me?
thanks in advance.
Nicky Eng.
11 2241
have a look at
http://pages.cpsc.ucalgary.ca/~kremer/STL/1024x768/index.html
http://www.josuttis.com/libbook/idx.html
thanks for the links.
but i still can't find out how to create template class like map container, something like this template<class Key, class T> without using any STL facilities.
thanks for the links.
but i still can't find out how to create template class like map container, something like this template<class Key, class T> without using any STL facilities.
for an example have a look at the CMapEx class
http://www.codeproject.com/cpp/extcol.asp
i want something like this: -
template<class K, class T>
-
class AssocArray {
-
public:
-
explicit inline AssocArray(int = 1); // constructor
-
inline T& operator[] (const K& k); // access element with key k
-
-
private:
-
int capacity;
-
K* k;
-
T* t;
-
};
-
/*** constrcutor ***/
-
template<class K, class T>
-
inline AssocArray<K, T>::AssocArray(int s) {
-
if(s <= 0) {
-
s = 1;
-
}
-
capacity = s;
-
k = new K[s];
-
// initialize variable t here, but i'm not sure how to initialise it.
-
}
-
-
template<class K, class T>
-
inline AssocArray<T>::operator[] (const AssocArray<K>& k) {
-
// do something here..
-
}
-
and the in the ccp file, i test the array, buy using this: -
AssocArray<string, int> arr;
-
string str;
-
int i;
-
-
arr[str] = i;
-
// of course must use other operators here,
-
//i just showing example and hope you guys understand what im trying to ask.
-
I CAN'T use any STL facilities in template class.
By the way, could anyone give example of code of what should i code in the comment part above, esp operator[] function.
thanks in advance.
Nicky Eng.
i want something like this:
I CAN'T use any STL facilities in template class.
By the way, could anyone give example of code of what should i code in the comment part above, esp operator[] function.
thanks in advance.
Nicky Eng.
have a look at
http://www.thescripts.com/forum/thread580637.html
are you looking for something like this? -
template<class K, class T>
-
class AssocArray {
-
public:
-
explicit inline AssocArray(int = 1); // constructor
-
inline T& operator[] (const K& k); // access element with key k
-
inline int size() { return asize;}
-
private:
-
int capacity;
-
int asize;
-
K* k;
-
T* t;
-
};
-
/*** constrcutor ***/
-
template<class K, class T>
-
inline AssocArray<K, T>::AssocArray(int s) {
-
if(s <= 0) {
-
s = 1;
-
}
-
capacity = s;
-
k = new K[s];
-
t = new T[s];
-
asize=0;
-
}
-
-
template<class K, class T>
-
inline T& AssocArray<K, T>::operator[] (const K& k1) {
-
// look for existing key - if found return reference to it
-
for(int i=0; i<asize;i++)
-
if(k[i]==k1) return t[i];
-
// add new key, return reference to it
-
k[asize]=k1;
-
return t[asize++];
-
}
-
if you try -
AssocArray<string, int> arr(10);
-
arr["one"]=1;
-
arr["two"]=2;
-
arr["three"]=3;
-
cout << arr["one"] << endl;
-
cout << arr["two"] << endl;
-
cout << arr["three"] << endl;
-
arr["two"]=20;
-
arr["three"]=300;
-
cout << arr["one"] << endl;
-
cout << arr["two"] << endl;
-
cout << arr["three"] << endl;
-
it gives
1
2
3
1
20
300
thanks horace1.
you helped me alot.
i will come back when i have questions.
thanks again.
:D
from
Nicky Eng.
hi ,
i had my template class created, and compiled well at the first place.
after i change some code, and then i regreted and change back to what it used to be.
and it give me this error: -
assocarray.h:54: internal compiler error: Segmentation fault
-
Please submit a full bug report,
-
with preprocessed source if appropriate
-
See <URL:http://gcc.gnu.org/bugs.html> for instructions
-
here is the code: -
#ifndef _ASSOCARRAY_H_
-
#define _ASSOCARRAY_H_
-
-
namespace Array {
-
-
template<class K, class T>
-
class AssocArray {
-
public:
-
explicit inline AssocArray(int = 1); // constructor
-
inline T& operator[] (K& k); // access element with key k
-
inline bool find (K& k);
-
inline int size() { return asize;}
-
private:
-
int capacity;
-
int asize;
-
K* k;
-
T* t;
-
};
-
-
-
/*** constrcutor ***/
-
template<class K, class T>
-
inline AssocArray<K, T>::AssocArray(int s) {
-
if(s <= 0) {
-
s = 1;
-
}
-
capacity = s;
-
k = new K[s];
-
t = new T[s];
-
asize=0;
-
}
-
-
template<class K, class T>
-
inline T& AssocArray<K, T>::operator[] (K& k1) {
-
// look for existing key - if found return reference to it
-
for(int i=0; i<asize;i++)
-
if(k[i]==k1) return t[i];
-
// add new key, return reference to it
-
k[asize]=k1;
-
return t[asize++];
-
}
-
-
template<class K, class T>
-
inline bool AssocArray<K, T>::find (K& k1) {
-
// look for existing key - if found return reference to it
-
for(int i=0; i<asize;i++) {
-
if(k[i]==k1) {
-
return true;
-
}
-
}
-
-
return false;
-
}
-
-
}
-
#endif
-
any ideas?
looks fines - I ran the following thru two gcc compilers without problems - the only change to your code is to make the parameter of operator[] a const so you can have expressions such as arr["one"] -
#include <iostream>
-
#include <string>
-
using namespace std;
-
-
#ifndef _ASSOCARRAY_H_
-
#define _ASSOCARRAY_H_
-
-
namespace Array {
-
-
template<class K, class T>
-
class AssocArray {
-
public:
-
explicit inline AssocArray(int = 1); // constructor
-
// ** added const
-
inline T& operator[] (const K& k); // access element with key k
-
inline bool find (K& k);
-
inline int size() { return asize;}
-
private:
-
int capacity;
-
int asize;
-
K* k;
-
T* t;
-
};
-
-
-
/*** constrcutor ***/
-
template<class K, class T>
-
inline AssocArray<K, T>::AssocArray(int s) {
-
if(s <= 0) {
-
s = 1;
-
}
-
capacity = s;
-
k = new K[s];
-
t = new T[s];
-
asize=0;
-
}
-
-
template<class K, class T>
-
inline T& AssocArray<K, T>::operator[] (const K& k1) { // ** added const
-
// look for existing key - if found return reference to it
-
for(int i=0; i<asize;i++)
-
if(k[i]==k1) return t[i];
-
// add new key, return reference to it
-
k[asize]=k1;
-
return t[asize++];
-
}
-
-
template<class K, class T>
-
inline bool AssocArray<K, T>::find (K& k1) {
-
// look for existing key - if found return reference to it
-
for(int i=0; i<asize;i++) {
-
if(k[i]==k1) {
-
return true;
-
}
-
}
-
-
return false;
-
}
-
-
}
-
-
#endif
-
-
using namespace Array;
-
-
int main()
-
{
-
AssocArray<string, int> arr(10);
-
arr["one"]=1;
-
arr["two"]=2;
-
arr["three"]=3;
-
cout << arr["one"] << endl;
-
cout << arr["two"] << endl;
-
cout << arr["three"] << endl;
-
arr["two"]=20;
-
arr["three"]=300;
-
cout << arr["one"] << endl;
-
cout << arr["two"] << endl;
-
cout << arr["three"] << endl;
-
cin.get();
-
}
-
thanks again.
i think it compiled okay. i mean preivous code.
What i have to do is ignore the "internal compiler error" issues.
i still can run my testfile.cpp.
thanks.
from
Nicky Eng
dear Horace1,
i noticed that it only can have 10 element.
what if i declare this: -
AssocArray<string, int> a;
-
i want when it reach the limit of capacity, it will double its allocated size each time run out of space. done in template class.
here is my code and it runs okay with declaration above i made. -
#ifndef _ASSOCARRAY_H_
-
#define _ASSOCARRAY_H_
-
#include <string>
-
-
namespace Array {
-
using std::string;
-
-
template<class K, class T>
-
class AssocArray {
-
public:
-
explicit inline AssocArray(int = 1);
-
inline T& operator[] (K& k); // non const
-
inline const T& operator[] (K& k) const; // const ref
-
inline int size() { return asize;}
-
private:
-
int capacity;
-
int asize;
-
K* k;
-
T* t;
-
inline void resize(int);
-
};
-
-
class BadSubscript {
-
public:
-
BadSubscript(const std::string& err) : msg(err){}
-
const std::string& Message() const { return msg; }
-
-
private:
-
const std::string msg;
-
};
-
-
/*** constrcutor ***/
-
template<class K, class T>
-
inline AssocArray<K, T>::AssocArray(int s) {
-
if(s <= 0) {
-
s = 1;
-
}
-
capacity = s;
-
k = new K[s];
-
t = new T[s];
-
asize=0;
-
}
-
-
template<class K, class T>
-
inline T& AssocArray<K, T>::operator[] (K& k1) {
-
-
if (asize >= capacity ) {
-
if (asize <= capacity*2 ) {
-
resize(capacity * 2);
-
} else {
-
resize(asize + 1);
-
}
-
}
-
k[asize] = k1;
-
return t[asize++];
-
}
-
-
template<class K, class T>
-
inline const T& AssocArray<K, T>::operator[] (K& k1) const{
-
// look for existing key - if found return reference to it
-
for(int i=0; i<asize;i++) {
-
if(k[i]==k1) {
-
return t[i];
-
}
-
}
-
throw BadSubscript("The word is not found! ");
-
}
-
-
template<class K, class T>
-
inline void AssocArray<K, T>::resize(int s) {
-
if (s > capacity) {
-
K* k1 = new K[s];
-
T* t1 = new T[s];
-
-
for(int i= 0; i < asize; i++) {
-
k1[i] = k[i];
-
t1[i] = t[i];
-
}
-
delete[] k;
-
delete[] t;
-
-
k = k1;
-
t = t1;
-
}
-
capacity = s;
-
}
-
}
-
#endif
-
specially thanks to horace1.
thanks alot.
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Patrick Kowalzick |
last post by:
Dear all,
I have a question about default template parameters.
I want to have a second template parameter which as a default parameter,
but depends on the first one (see below). Is something...
|
by: Sebastian Faust |
last post by:
Hi,
I have 4 questions related to templates.
I wanna do something like the following:
template<typename T>
class Template
{
public:
Template_Test<T>()
|
by: Oplec |
last post by:
Hi,
I'm learning C++ as a hobby using The C++ Programming Language : Special
Edition by Bjarne Stroustrup. I'm working on chpater 13 exercises that
deal with templates. Exercise 13.9 asks for me...
|
by: Nobody |
last post by:
This is sort of my first attempt at writing a template container class, just
wanted some feedback if everything looks kosher or if there can be any
improvements. This is a template class for a...
|
by: Leslaw Bieniasz |
last post by:
Cracow, 16.09.2004
Hi,
I have a problem with compiling the following construction involving
cross-calls of class template methods, with additional inheritance.
I want to have three class...
|
by: gao_bolin |
last post by:
I am facing the following scenario: I have a class 'A', that implements
some concept C -- but we know this, not because A inherits from a
virtual class 'C', but only because a trait tell us so:
...
|
by: Rudy Ray Moore |
last post by:
Whenever I get any error with Vc++7.1/.net/2003, it is followed by huge
ammounts of "template assistance" error messaging referencing template code
(MTL) that has nothing to do with the error.
...
|
by: Alfonso Morra |
last post by:
I have a class declared as ff:
class __declspec(dllexport) A {
public:
A() ;
A(const A&)
A& operator=(const A&) ;
~A() ;
void doThis(void) ;
|
by: Hamilton Woods |
last post by:
Diehards,
I developed a template matrix class back around 1992 using Borland C++ 4.5
(ancestor of C++ Builder) and haven't touched it until a few days ago. I
pulled it from the freezer and...
|
by: charles.lobo |
last post by:
Hi,
I have recently begun using templates in C++ and have found it to be
quite useful. However, hearing stories of code bloat and assorted
problems I decided to write a couple of small programs...
|
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,...
|
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...
|
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...
|
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
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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,...
|
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...
|
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,...
| |