473,503 Members | 1,857 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

STL MAP, how to restrict

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

using namespace std;
void InitMap( map<string, int>& Map)
{
Map["aa"] = 0;
Map["bb"] = 0;
}

int main() {

map<string, intMyMap;

InitMap( MyMap);

MyMap["aa"] = 10;// Questione 1

MyMap["cc"] = 20;// Question 2

map<string, int>::const_iterator iter;
for (iter=MyMap.begin(); iter != MyMap.end(); iter++) {
cout << iter->first << " " << iter->second << endl;
}

return 0;

}
Question 1:

How do I create my own map, which would allow me to check during

MyMap["aa"] = 10;

that values for "aa" can be from 0 to 5, so the above assignment
would print error message.
Question 2:

How do I prevent my map to be only 2 elements, "aa" and "bb", so
creation of new element "cc" would not be allowed.

Thanks,
-haro
(I tried and failed, I googled and failed, so I came here.
Please excuse if this is very simple or discribed somewhere)
Nov 15 '06 #1
5 1716
Haro Panosyan wrote:
#include <iostream>
#include <map>
#include <string>

using namespace std;
void InitMap( map<string, int>& Map)
{
Map["aa"] = 0;
Map["bb"] = 0;
}

int main() {

map<string, intMyMap;

InitMap( MyMap);

MyMap["aa"] = 10;// Questione 1

MyMap["cc"] = 20;// Question 2

map<string, int>::const_iterator iter;
for (iter=MyMap.begin(); iter != MyMap.end(); iter++) {
cout << iter->first << " " << iter->second << endl;
}

return 0;

}
Question 1:

How do I create my own map, which would allow me to check during

MyMap["aa"] = 10;

that values for "aa" can be from 0 to 5, so the above assignment
would print error message.
Question 2:

How do I prevent my map to be only 2 elements, "aa" and "bb", so
creation of new element "cc" would not be allowed.

Thanks,
-haro
(I tried and failed, I googled and failed, so I came here.
Please excuse if this is very simple or discribed somewhere)
What you seem to need is a "wrapper" for 'std::map'. Since your
own 'map' is not an extension of 'std::map' (you want to impose
limitations on 'std::map' contents, which means your map is not
a std::map in the pure OO sense, see LSP), you should consider
private inheritance and re-implementation of the interface with
pre- and post-conditions verified in your implementations.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Nov 15 '06 #2

Haro Panosyan wrote:
>
How do I create my own map, which would allow me to check during

MyMap["aa"] = 10;

that values for "aa" can be from 0 to 5, so the above assignment
would print error message.
Question 2:

How do I prevent my map to be only 2 elements, "aa" and "bb", so
creation of new element "cc" would not be allowed.
It sounds to me like a map is totally inappropriate for your needs.
You should consider a different container or even no container at all.
You're using none of map's features.

Nov 15 '06 #3
Haro Panosyan wrote:
#include <iostream>
#include <map>
#include <string>

using namespace std;
void InitMap( map<string, int>& Map)
{
Map["aa"] = 0;
Map["bb"] = 0;
}

int main() {

map<string, intMyMap;

InitMap( MyMap);

MyMap["aa"] = 10;// Questione 1

MyMap["cc"] = 20;// Question 2

map<string, int>::const_iterator iter;
for (iter=MyMap.begin(); iter != MyMap.end(); iter++) {
cout << iter->first << " " << iter->second << endl;
}

return 0;

}
Question 1:

How do I create my own map, which would allow me to check during

MyMap["aa"] = 10;

that values for "aa" can be from 0 to 5, so the above assignment
would print error message.
Create a class that does range checking and automatically converts to
and from int, e.g. (untested code):

#include <map>
#include <string>
#include <exception>

template<typename T, T lower, T upper>
class RangeCheckedVal
{
T val_;

static T Validate( const T& val )
{
if( val < lower || val upper )
{
throw std::range_error( "Value out of range" );
}
return val;
}

public:
RangeCheckedVal( const T& val = lower )
: val_( Validate(val) )
{}

operator T() const { return val_; }
};

int main()
{
std::map<std::string, RangeCheckedVal<int,0,5 myMap;
myMap[ "aa" ] = 5; // ok
myMap[ "bb" ] = 10; // throws an exception
}
Question 2:

How do I prevent my map to be only 2 elements, "aa" and "bb", so
creation of new element "cc" would not be allowed.
Make the map const. To do this, you'd need to initialize the class on
declaration somehow, e.g.:

template<class K, class V>
class MapInitializer
{
typedef std::map<K,VMap;
Map m_;
public:
operator Map() const { return m_; }

MapInitializer& Add( const K& k, const V& v )
{
m_[k] = v;
return *this;
}
};

const std::map<std::string,intmyMap
= MapInitializer<std::string, int>()
.Add( "aa", 9 )
.Add( "bb", 42 );

Of course, if the map is const, the [] operator won't work (outside the
initializer) since it inserts a key/value pair if one doesn't exist,
but std::map::find() is safer anyway.

Cheers! --M

Nov 15 '06 #4
Please find my comments bellow inserted.

mlimber wrote:
Haro Panosyan wrote:
>>#include <iostream>
#include <map>
#include <string>

using namespace std;
void InitMap( map<string, int>& Map)
{
Map["aa"] = 0;
Map["bb"] = 0;
}

int main() {

map<string, intMyMap;

InitMap( MyMap);

MyMap["aa"] = 10;// Questione 1

MyMap["cc"] = 20;// Question 2

map<string, int>::const_iterator iter;
for (iter=MyMap.begin(); iter != MyMap.end(); iter++) {
cout << iter->first << " " << iter->second << endl;
}

return 0;

}
Question 1:

How do I create my own map, which would allow me to check during

MyMap["aa"] = 10;

that values for "aa" can be from 0 to 5, so the above assignment
would print error message.


Create a class that does range checking and automatically converts to
and from int, e.g. (untested code):

#include <map>
#include <string>
#include <exception>

template<typename T, T lower, T upper>
class RangeCheckedVal
{
T val_;

static T Validate( const T& val )
{
if( val < lower || val upper )
{
throw std::range_error( "Value out of range" );
}
return val;
}

public:
RangeCheckedVal( const T& val = lower )
: val_( Validate(val) )
{}

operator T() const { return val_; }
};

int main()
{
std::map<std::string, RangeCheckedVal<int,0,5 myMap;
myMap[ "aa" ] = 5; // ok
myMap[ "bb" ] = 10; // throws an exception
}

Thank,
This is nice, but I think this way both "aa" and "bb" would have
same range. What I wanted, is somehow to overwrite assignment, and
check if "aa" then range is 0 to 5, if "bb" then range is 3 to 9
for example.
>>Question 2:

How do I prevent my map to be only 2 elements, "aa" and "bb", so
creation of new element "cc" would not be allowed.


Make the map const. To do this, you'd need to initialize the class on
declaration somehow, e.g.:

template<class K, class V>
class MapInitializer
{
typedef std::map<K,VMap;
Map m_;
public:
operator Map() const { return m_; }

MapInitializer& Add( const K& k, const V& v )
{
m_[k] = v;
return *this;
}
};

const std::map<std::string,intmyMap
= MapInitializer<std::string, int>()
.Add( "aa", 9 )
.Add( "bb", 42 );

Of course, if the map is const, the [] operator won't work (outside the
initializer) since it inserts a key/value pair if one doesn't exist,
but std::map::find() is safer anyway.

Cheers! --M
I will need more time to exercise this.
Nov 15 '06 #5
Haro Panosyan wrote:
>Question 1:

How do I create my own map, which would allow me to check during

MyMap["aa"] = 10;

that values for "aa" can be from 0 to 5, so the above assignment
would print error message.

Create a class that does range checking and automatically converts to
and from int, e.g. (untested code):

#include <map>
#include <string>
#include <exception>

template<typename T, T lower, T upper>
class RangeCheckedVal
{
T val_;

static T Validate( const T& val )
{
if( val < lower || val upper )
{
throw std::range_error( "Value out of range" );
}
return val;
}

public:
RangeCheckedVal( const T& val = lower )
: val_( Validate(val) )
{}

operator T() const { return val_; }
};

int main()
{
std::map<std::string, RangeCheckedVal<int,0,5 myMap;
myMap[ "aa" ] = 5; // ok
myMap[ "bb" ] = 10; // throws an exception
}

Thank,
This is nice, but I think this way both "aa" and "bb" would have
same range. What I wanted, is somehow to overwrite assignment, and
check if "aa" then range is 0 to 5, if "bb" then range is 3 to 9
for example.
Then you'll probably want to implement your own class that encapsulates
a map (or some other data structure).

Cheers! --M

Nov 15 '06 #6

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

Similar topics

28
6383
by: gc | last post by:
Hi, What is the purpose of the restrict keyword? gc
4
2171
by: Vijay Kumar R Zanvar | last post by:
Greetings, Are the following inferences of mine correct? 1. #include <string.h> char *strcpy(char * restrict s1, const char * restrict s2); a. s1 != s2 b. That means,
7
2658
by: tweak | last post by:
Can someone give me a short example as how to best use this keyword in your code? This is my understanding: by definition restrict sounds like it is suppose to restrict access to memory...
1
2491
by: ccdrbrg | last post by:
I'm having trouble understanding restrict. Can someone provide a layman's explanation. Chad
2
2394
by: pemo | last post by:
In Harbison and Steele's book, they say that using 'restrict' allows functions like memcpy() to be prototyped like this: void * memcpy(void * restrict s1, const void * restrict s2, size_t n); ...
12
2471
by: Me | last post by:
I'm trying to wrap my head around the wording but from what I think the standard says: 1. it's impossible to swap a restrict pointer with another pointer, i.e. int a = 1, b = 2; int *...
21
6469
by: Niu Xiao | last post by:
I see a lot of use in function declarations, such as size_t fread(void* restrict ptr, size_t size, size_t nobj, FILE* restrict fp); but what does the keyword 'restrict' mean? there is no...
2
3242
by: venkat | last post by:
Hi, i came across restrict qualifier while looking the code. I haven't able to understand what does this do?. Can some one help me how does this makes the things restrict to an specified...
0
2966
by: copx | last post by:
Restrict keyword questions How far does the guarantee that an object is not accessed through another pointer go? I mean, all examples I have seen are simple stuff like: int f (int *restrict x,...
23
4799
by: raashid bhatt | last post by:
what is restrict keyword used for? eg int *restrict p;
0
7093
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
7287
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,...
1
7006
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...
0
7467
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...
0
3175
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3166
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1519
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 ...
1
744
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
397
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...

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.