473,325 Members | 2,860 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,325 software developers and data experts.

any idear?

I want a class Table which saves one list of Pair<int,std::string>.
And I want that it has following behaviour:

Table t;//now t is empty
try{
int i = t["me"];// I want this call to an empty table will throw a
exception [case 1]
}
catch(...){
std::cout << "It's a empty table,bad operator" << std::endl;
t["me"] = 0;//and i hope this call can work well [case 2]
}
Could some one tell me how to define this class Table?
How to define operator for case 1 and case 2?

class Table
{
public:
class Bad_op{};
struct Pair
{
int data;
std::string str;
};
Table(const Table& table);
Table();
~Table();
Table& operator= (const Table& table);
//int& operator[](const std::string& str);???????
//nst int& operator[](const std::string& str)const;???????
private:
std::vector<Pairm_Vec;
};

Oct 17 '08 #1
4 1391
Hill wrote:
I want a class Table which saves one list of Pair<int,std::string>.
And I want that it has following behaviour:

Table t;//now t is empty
try{
int i = t["me"];// I want this call to an empty table will throw a
exception [case 1]
}
catch(...){
std::cout << "It's a empty table,bad operator" << std::endl;
t["me"] = 0;//and i hope this call can work well [case 2]
}
First, a clarification about the specs: what about

int i = t["here"];

where t is not empty but no entry for "here" is to be found?
>
Could some one tell me how to define this class Table?
How to define operator for case 1 and case 2?

class Table
{
public:
class Bad_op{};
struct Pair
{
int data;
std::string str;
};
Table(const Table& table);
Table();
~Table();
Table& operator= (const Table& table);
//int& operator[](const std::string& str);???????
//nst int& operator[](const std::string& str)const;???????
private:
std::vector<Pairm_Vec;
};
Why a vector of pairs? It seems that all you need is a thin wrapper around
std::map< std::string, int >.
Best

Kai-Uwe Bux
Oct 17 '08 #2
On 10ÔÂ17ÈÕ, ÏÂÎç2ʱ08·Ö, Kai-Uwe Bux <jkherci...@gmx.netwrote:
Hill wrote:
I want a class Table which saves one list of Pair<int,std::string>.
And I want that it has following behaviour:
Table t;//now t is empty
try{
int i = t["me"];// I want this call to an empty table will throw a
exception [case 1]
}
catch(...){
std::cout << "It's a empty table,bad operator" << std::endl;
t["me"] = 0;//and i hope this call can work well [case 2]
}

First, a clarification about the specs: what about

int i = t["here"];

where t is not empty but no entry for "here" is to be found?


Could some one tell me how to define this class Table?
How to define operator for case 1 and case 2?
class Table
{
public:
class Bad_op{};
struct Pair
{
int data;
std::string str;
};
Table(const Table& table);
Table();
~Table();
Table& operator= (const Table& table);
//int& operator[](const std::string& str);???????
//nst int& operator[](const std::string& str)const;???????
private:
std::vector<Pairm_Vec;
};

Why a vector of pairs? It seems that all you need is a thin wrapper around
std::map< std::string, int >.

Best

Kai-Uwe Bux- Òþ²Ø±»ÒýÓÃÎÄ×Ö -

- ÏÔʾÒýÓõÄÎÄ×Ö -
Yes , When i say "empty" , i mean the table has a emplty list.
It's just a exercise. Not realistic class.
Oct 17 '08 #3
Hill wrote:
On 10?17?, ??2?08?, Kai-Uwe Bux <jkherci...@gmx.netwrote:
>Hill wrote:
I want a class Table which saves one list of Pair<int,std::string>.
And I want that it has following behaviour:
Table t;//now t is empty
try{
int i = t["me"];// I want this call to an empty table will throw a
exception [case 1]
}
catch(...){
std::cout << "It's a empty table,bad operator" << std::endl;
t["me"] = 0;//and i hope this call can work well [case 2]
}

First, a clarification about the specs: what about

int i = t["here"];

where t is not empty but no entry for "here" is to be found?


Could some one tell me how to define this class Table?
How to define operator for case 1 and case 2?
class Table
{
public:
class Bad_op{};
struct Pair
{
int data;
std::string str;
};
Table(const Table& table);
Table();
~Table();
Table& operator= (const Table& table);
//int& operator[](const std::string& str);???????
//nst int& operator[](const std::string& str)const;???????
private:
std::vector<Pairm_Vec;
};

Why a vector of pairs? It seems that all you need is a thin wrapper
around std::map< std::string, int >.

Best

Kai-Uwe Bux- ??????? -

- ??????? -

Yes , When i say "empty" , i mean the table has a emplty list.
It's just a exercise. Not realistic class.
In that case, you could try something like this:
#include <map>
#include <stdexcept>
#include <string>
#include <iostream>

template < typename Key, typename Mapped >
class table : public std::map< Key, Mapped {

struct proxy {

table * the_table;
Key the_key;

proxy ( table * ptr, Key const & key )
: the_table ( ptr )
, the_key ( key )
{}

proxy & operator= ( Mapped const & rhs ) {
static_cast< std::map< Key, Mapped * >
( the_table )->operator[]( the_key ) = rhs;
return ( *this );
}

operator Mapped & ( void ) const {
if ( the_table->empty() ) {
throw ( std::exception() );
}
return ( static_cast< std::map< Key, Mapped * >
( the_table )->operator[]( the_key ) );
}

};

struct const_proxy {

table * the_table;
Key the_key;

const_proxy ( table * ptr, Key const & key )
: the_table ( ptr )
, the_key ( key )
{}

operator Mapped const & ( void ) const {
if ( the_table->empty() ) {
throw ( std::exception() );
}
return ( static_cast< std::map< Key, Mapped * >
( the_table )->operator[]( the_key ) );
}

};

public:

table ( void ) :
std::map< Key, Mapped ()
{}

template < typename A >
table ( A a ) :
std::map< Key, Mapped ( a )
{}

template < typename A, typename B >
table ( A a, B b ) :
std::map< Key, Mapped ( a, b )
{}

proxy operator[] ( Key const & key ) {
return ( proxy( this, key ) );
}

const_proxy operator[] ( Key const & key ) const {
return ( proxy( this, key ) );
}

};

int main ( void ) {
table< std::string, int t;
{
try{
int i = t[ "me" ];
}
catch(...){
std::cout << "It's a empty table,bad operator" << std::endl;
}
}
{
t[ "me" ] = 0;
}
{
try{
int i = t[ "me" ];
}
catch(...){
std::cout << "It's a empty table,bad operator" << std::endl;
}
}
}
Best

Kai-Uwe Bux
Oct 17 '08 #4
On 10ÔÂ17ÈÕ, ÏÂÎç4ʱ26·Ö, Kai-Uwe Bux <jkherci...@gmx.netwrote:
Hill wrote:
On 10?17?, ??2?08?, Kai-Uwe Bux <jkherci...@gmx.netwrote:
Hill wrote:
I want a class Table which saves one list of Pair<int,std::string>.
And I want that it has following behaviour:
Table t;//now t is empty
try{
int i = t["me"];// I want this call to an empty table will throw a
exception [case 1]
}
catch(...){
std::cout << "It's a empty table,bad operator" << std::endl;
t["me"] = 0;//and i hope this call can work well [case 2]
}
First, a clarification about the specs: what about
int i = t["here"];
where t is not empty but no entry for "here" is to be found?
Could some one tell me how to define this class Table?
How to define operator for case 1 and case 2?
class Table
{
public:
class Bad_op{};
struct Pair
{
int data;
std::string str;
};
Table(const Table& table);
Table();
~Table();
Table& operator= (const Table& table);
//int& operator[](const std::string& str);???????
//nst int& operator[](const std::string& str)const;???????
private:
std::vector<Pairm_Vec;
};
Why a vector of pairs? It seems that all you need is a thin wrapper
around std::map< std::string, int >.
Best
Kai-Uwe Bux- ??????? -
- ??????? -
Yes , When i say "empty" , i mean the table has a emplty list.
It's just a exercise. Not realistic class.

In that case, you could try something like this:

#include <map>
#include <stdexcept>
#include <string>
#include <iostream>

template < typename Key, typename Mapped >
class table : public std::map< Key, Mapped {

struct proxy {

table * the_table;
Key the_key;

proxy ( table * ptr, Key const & key )
: the_table ( ptr )
, the_key ( key )
{}

proxy & operator= ( Mapped const & rhs ) {
static_cast< std::map< Key, Mapped * >
( the_table )->operator[]( the_key ) = rhs;
return ( *this );
}

operator Mapped & ( void ) const {
if ( the_table->empty() ) {
throw ( std::exception() );
}
return ( static_cast< std::map< Key, Mapped * >
( the_table )->operator[]( the_key ) );
}

};

struct const_proxy {

table * the_table;
Key the_key;

const_proxy ( table * ptr, Key const & key )
: the_table ( ptr )
, the_key ( key )
{}

operator Mapped const & ( void ) const {
if ( the_table->empty() ) {
throw ( std::exception() );
}
return ( static_cast< std::map< Key, Mapped * >
( the_table )->operator[]( the_key ) );
}

};

public:

table ( void ) :
std::map< Key, Mapped ()
{}

template < typename A >
table ( A a ) :
std::map< Key, Mapped ( a )
{}

template < typename A, typename B >
table ( A a, B b ) :
std::map< Key, Mapped ( a, b )
{}

proxy operator[] ( Key const & key ) {
return ( proxy( this, key ) );
}

const_proxy operator[] ( Key const & key ) const {
return ( proxy( this, key ) );
}

};

int main ( void ) {
table< std::string, int t;
{
try{
int i = t[ "me" ];
}
catch(...){
std::cout << "It's a empty table,bad operator" << std::endl;
}
}
{
t[ "me" ] = 0;
}
{
try{
int i = t[ "me" ];
}
catch(...){
std::cout << "It's a empty table,bad operator" << std::endl;
}
}

}

Best

Kai-Uwe Bux- Òþ²Ø±»ÒýÓÃÎÄ×Ö -

- ÏÔʾÒýÓõÄÎÄ×Ö -
By overloading operator= and operator Mapped& , We distinguish the
lvalue and rvalue.
It's a good idear. Thanks.
Oct 17 '08 #5

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

Similar topics

3
by: Bennie | last post by:
Hi, I can resize in IE whit window.resizeTo(widht,height) but it don't work for Netscape? Have try location.reload() in Netscape. But this don't work.... Any idear? Bennie,
1
by: Denis Correard | last post by:
I'm trying to create a page that automaticaly send email. I'm using the system.web.mail namespace but from time to time i'm getting this error: Could not access 'CDO.Message' object. Same time...
0
by: RamseytheScot | last post by:
At the moment we have a httphandler. This handler connects to services and redirect messages to this service. To use this service you have to log on using a Username and Password. This Username and...
4
by: sparks | last post by:
As you might know that "java.lang" package are automatically imported by the java compiler so that one don't need to write the import statement of that package in the source code. Are there any...
1
by: Andreas Horneff | last post by:
Hi at all, I've got a problem. I've a program written in Borland C++ Builder which is running on a small PC with a Touch-Screen. At the program are several Edit-Boxes. Is there a posibility,...
6
by: Jens | last post by:
I have a database file LEAR_Index(yes, it hold index data) from a have have recently removed a bunch of data. It is about 120 Gb, 100Gb of which is not used. I wan´t to shrink the file to lean...
4
by: Peloux | last post by:
Hi, I have written some htc in order to validate data in a form. most of htc are attached on 'onblur' event. Now, we would like to use the Enter Key to sublit form, so we use the following...
2
by: Shads | last post by:
Hi, I receive this error in the System Event Log? Event Type: Error Event Source: Service Control Manager Event Category: None Event ID: 7031 Date: 15/08/2005 Time: 22:22:32
13
by: iwdu15 | last post by:
hi, im 16 and been programming in VB.net for about a year, and am almost certain that im going to major in CS in college...but i was wondering which language should i be most familiar with if i do...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.