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

map template class

254 100+
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.
Jan 5 '07 #1
11 2241
horace1
1,510 Expert 1GB
have a look at
http://pages.cpsc.ucalgary.ca/~kremer/STL/1024x768/index.html
http://www.josuttis.com/libbook/idx.html
Jan 5 '07 #2
nickyeng
254 100+
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.
Jan 5 '07 #3
horace1
1,510 Expert 1GB
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
Jan 6 '07 #4
nickyeng
254 100+
i want something like this:
Expand|Select|Wrap|Line Numbers
  1. template<class K, class T>
  2.     class AssocArray {
  3.     public:
  4.         explicit inline AssocArray(int = 1);    // constructor
  5.         inline T& operator[] (const K& k);    // access element with key k
  6.  
  7.       private:
  8.         int capacity;
  9.         K* k;
  10.         T* t;
  11.         };
  12.          /*** constrcutor ***/
  13.         template<class K, class T>
  14.         inline AssocArray<K, T>::AssocArray(int s) {
  15.         if(s <= 0) {
  16.             s = 1;
  17.         }
  18.         capacity = s;
  19.         k = new K[s];
  20.         // initialize variable t here, but i'm not sure how to initialise it.
  21.     }
  22.  
  23.         template<class K, class T>
  24.     inline AssocArray<T>::operator[] (const AssocArray<K>& k) {
  25.         // do something here..
  26.     }
  27.  
and the in the ccp file, i test the array, buy using this:
Expand|Select|Wrap|Line Numbers
  1. AssocArray<string, int> arr;
  2. string str;
  3. int i;
  4.  
  5. arr[str] = i;
  6. // of course must use other operators here, 
  7. //i just showing example and hope you guys understand what im trying to ask.
  8.  
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.
Jan 6 '07 #5
horace1
1,510 Expert 1GB
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
Jan 6 '07 #6
horace1
1,510 Expert 1GB
are you looking for something like this?
Expand|Select|Wrap|Line Numbers
  1. template<class K, class T>
  2.     class AssocArray {
  3.     public:
  4.         explicit inline AssocArray(int = 1);    // constructor
  5.         inline T& operator[] (const K& k);    // access element with key k
  6.         inline int size() { return asize;}
  7.       private:
  8.         int capacity;
  9.         int asize;
  10.         K* k;
  11.         T* t;
  12.         };
  13.          /*** constrcutor ***/
  14.         template<class K, class T>
  15.         inline AssocArray<K, T>::AssocArray(int s) {
  16.         if(s <= 0) {
  17.             s = 1;
  18.         }
  19.         capacity = s;
  20.         k = new K[s];
  21.         t = new T[s];
  22.         asize=0;
  23.     }
  24.  
  25.    template<class K, class T>
  26.     inline T& AssocArray<K, T>::operator[] (const K& k1) {
  27.            // look for existing key - if found return reference to  it
  28.            for(int i=0; i<asize;i++)
  29.               if(k[i]==k1) return t[i];
  30.            // add new key, return reference to it
  31.            k[asize]=k1;
  32.            return t[asize++];
  33.     }
  34.  
if you try
Expand|Select|Wrap|Line Numbers
  1.     AssocArray<string, int> arr(10);
  2.     arr["one"]=1;
  3.     arr["two"]=2;
  4.     arr["three"]=3;
  5.     cout << arr["one"] << endl;
  6.     cout << arr["two"] << endl;
  7.     cout << arr["three"] << endl;
  8.     arr["two"]=20;
  9.     arr["three"]=300;
  10.     cout << arr["one"] << endl;
  11.     cout << arr["two"] << endl;
  12.     cout << arr["three"] << endl;
  13.  
it gives
1
2
3
1
20
300
Jan 6 '07 #7
nickyeng
254 100+
thanks horace1.

you helped me alot.

i will come back when i have questions.

thanks again.
:D

from
Nicky Eng.
Jan 6 '07 #8
nickyeng
254 100+
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:
Expand|Select|Wrap|Line Numbers
  1. assocarray.h:54: internal compiler error: Segmentation fault
  2. Please submit a full bug report, 
  3. with preprocessed source if appropriate
  4. See <URL:http://gcc.gnu.org/bugs.html> for instructions
  5.  
here is the code:
Expand|Select|Wrap|Line Numbers
  1. #ifndef _ASSOCARRAY_H_
  2. #define _ASSOCARRAY_H_
  3.  
  4. namespace Array {
  5.  
  6.     template<class K, class T>
  7.     class AssocArray {
  8.     public:
  9.         explicit inline AssocArray(int = 1);    // constructor
  10.         inline T& operator[] (K& k);    // access element with key k
  11.         inline bool find (K& k);
  12.         inline int size() { return asize;}
  13.     private:
  14.         int capacity;
  15.         int asize;
  16.         K* k;
  17.         T* t;
  18.     };
  19.  
  20.  
  21.     /*** constrcutor ***/
  22.     template<class K, class T>
  23.         inline AssocArray<K, T>::AssocArray(int s) {
  24.             if(s <= 0) {
  25.                 s = 1;
  26.             }
  27.             capacity = s;
  28.             k = new K[s];
  29.             t = new T[s];
  30.             asize=0;
  31.         }
  32.  
  33.     template<class K, class T>
  34.         inline T& AssocArray<K, T>::operator[] (K& k1) {
  35.             // look for existing key - if found return reference to  it
  36.             for(int i=0; i<asize;i++)
  37.                 if(k[i]==k1) return t[i];
  38.             // add new key, return reference to it
  39.                 k[asize]=k1;
  40.                 return t[asize++];
  41.     }
  42.  
  43.     template<class K, class T>
  44.     inline bool AssocArray<K, T>::find (K& k1) {
  45.         // look for existing key - if found return reference to  it
  46.         for(int i=0; i<asize;i++) {
  47.             if(k[i]==k1) {
  48.                 return true;
  49.             }
  50.         }
  51.  
  52.         return false;
  53.     }
  54.  
  55. }
  56. #endif
  57.  
any ideas?
Jan 6 '07 #9
horace1
1,510 Expert 1GB
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"]
Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <string>
  3. using namespace std;
  4.  
  5. #ifndef _ASSOCARRAY_H_
  6. #define _ASSOCARRAY_H_
  7.  
  8. namespace Array {
  9.  
  10.     template<class K, class T>
  11.     class AssocArray {
  12.     public:
  13.         explicit inline AssocArray(int = 1);    // constructor
  14.           // ** added const
  15.         inline T& operator[] (const K& k);    // access element with key k
  16.         inline bool find (K& k);
  17.         inline int size() { return asize;}
  18.     private:
  19.         int capacity;
  20.         int asize;
  21.         K* k;
  22.         T* t;
  23.     };
  24.  
  25.  
  26.     /*** constrcutor ***/
  27.     template<class K, class T>
  28.         inline AssocArray<K, T>::AssocArray(int s) {
  29.             if(s <= 0) {
  30.                 s = 1;
  31.             }
  32.             capacity = s;
  33.             k = new K[s];
  34.             t = new T[s];
  35.             asize=0;
  36.         }
  37.  
  38.     template<class K, class T>
  39.         inline T& AssocArray<K, T>::operator[] (const K& k1) {  // ** added const
  40.             // look for existing key - if found return reference to  it
  41.             for(int i=0; i<asize;i++)
  42.                 if(k[i]==k1) return t[i];
  43.             // add new key, return reference to it
  44.                 k[asize]=k1;
  45.                 return t[asize++];
  46.     }
  47.  
  48.     template<class K, class T>
  49.     inline bool AssocArray<K, T>::find (K& k1) {
  50.         // look for existing key - if found return reference to  it
  51.         for(int i=0; i<asize;i++) {
  52.             if(k[i]==k1) {
  53.                 return true;
  54.             }
  55.         }
  56.  
  57.         return false;
  58.     }
  59.  
  60. }
  61.  
  62. #endif
  63.  
  64. using namespace Array;
  65.  
  66. int main()
  67. {
  68.     AssocArray<string, int> arr(10);
  69.     arr["one"]=1;
  70.     arr["two"]=2;
  71.     arr["three"]=3;
  72.     cout << arr["one"] << endl;
  73.     cout << arr["two"] << endl;
  74.     cout << arr["three"] << endl;
  75.     arr["two"]=20;
  76.     arr["three"]=300;
  77.     cout << arr["one"] << endl;
  78.     cout << arr["two"] << endl;
  79.     cout << arr["three"] << endl;
  80.     cin.get();
  81. }
  82.  
Jan 7 '07 #10
nickyeng
254 100+
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
Jan 7 '07 #11
nickyeng
254 100+
dear Horace1,

i noticed that it only can have 10 element.

what if i declare this:
Expand|Select|Wrap|Line Numbers
  1. AssocArray<string, int> a;
  2.  
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.
Expand|Select|Wrap|Line Numbers
  1. #ifndef _ASSOCARRAY_H_
  2. #define _ASSOCARRAY_H_
  3. #include <string>
  4.  
  5. namespace Array {
  6.     using std::string;
  7.  
  8.     template<class K, class T>
  9.     class AssocArray {
  10.     public:
  11.         explicit inline AssocArray(int = 1);  
  12.         inline T& operator[] (K& k);    // non const
  13.         inline const T& operator[] (K& k) const;    // const ref
  14.         inline int size() { return asize;}
  15.     private:
  16.         int capacity;
  17.         int asize;
  18.         K* k;
  19.         T* t;
  20.         inline void resize(int);
  21.     };
  22.  
  23.     class BadSubscript {
  24.     public:
  25.         BadSubscript(const std::string& err) : msg(err){}
  26.         const std::string& Message() const { return msg; }
  27.  
  28.     private:
  29.             const std::string msg;
  30.     };
  31.  
  32.     /*** constrcutor ***/
  33.     template<class K, class T>
  34.     inline AssocArray<K, T>::AssocArray(int s) {
  35.         if(s <= 0) {
  36.             s = 1;
  37.         }
  38.         capacity = s;
  39.         k = new K[s];
  40.         t = new T[s];
  41.         asize=0;
  42.     }
  43.  
  44.     template<class K, class T>
  45.     inline T& AssocArray<K, T>::operator[] (K& k1) {
  46.  
  47.         if (asize >= capacity ) {
  48.             if (asize <= capacity*2 ) {
  49.                 resize(capacity * 2);
  50.             } else {
  51.                 resize(asize + 1);
  52.             }
  53.         }
  54.             k[asize] = k1;
  55.             return t[asize++];
  56.     }
  57.  
  58.     template<class K, class T>
  59.     inline const T& AssocArray<K, T>::operator[] (K& k1) const{
  60.         // look for existing key - if found return reference to  it
  61.         for(int i=0; i<asize;i++) {
  62.             if(k[i]==k1) {
  63.                 return t[i];
  64.             }
  65.         }
  66.         throw BadSubscript("The word is not found! ");
  67.     }
  68.  
  69.     template<class K, class T>
  70.     inline void AssocArray<K, T>::resize(int s) {
  71.         if (s > capacity) {
  72.             K* k1 = new K[s];
  73.             T* t1 = new T[s];
  74.  
  75.             for(int i= 0; i < asize; i++) {
  76.                 k1[i] = k[i];
  77.                 t1[i] = t[i];
  78.             }
  79.             delete[] k;
  80.             delete[] t;
  81.  
  82.             k = k1;
  83.             t = t1;
  84.         }
  85.         capacity = s;
  86.     }
  87. }
  88. #endif
  89.  
specially thanks to horace1.
thanks alot.
Jan 9 '07 #12

Sign in to post your reply or Sign up for a free account.

Similar topics

6
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...
4
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>()
1
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...
6
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...
0
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...
11
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: ...
2
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. ...
2
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) ;
3
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...
45
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...
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: 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: 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
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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.