473,544 Members | 1,539 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Should one use std::string*, std::map* to avoid copying ?

This is a very simplified version of something I am trying to
understand.
The State object holds the strings and maps , and I pass a reference
to State to the process Function which manipulates it based on the tag
passed in.

I cannot pass the strings/maps in question as references coz ..
a) thats redundant, state has everything, it increases number of
parameters unnecessarily
b) I have to do the case/switch outside the function to decide what to
pass in.( and this is a simple example )

Is it a good idea to use things like string* and std::map..* as i have
tried below

Thanks
Digz

------------------------
#include<string >
#include<map>

using namespace std;
typedef std::map<int, stringmapType;

struct State {
string pId_;
mapType pTypeNSymbol_;
string uId_;
mapType uTypeNSymbol_;
};

void process( State& state, int Tag){
typedef mapType::iterat or mItr ;
string* prodId;
mapType* typeSymMap;

switch( Tag ) {
case 0:
prodId = &state.pId_;
typeSymMap = &state.pTypeNSy mbol_;
break;
case 1:
prodId = &state.uId_;
typeSymMap = &state.uTypeNSy mbol_;
break;
}
if (prodId->empty()) {
mItr it;
mItr end = typeSymMap->end();
if ( (it = typeSymMap->find(6)) != end){
*prodId = (*typeSymMap)[6];
}
}

if (!typeSymMap->empty()) {
mItr it = typeSymMap->begin();
*prodId = it->second;
}
}
Jan 8 '08 #1
3 2044
On Jan 8, 5:25 pm, digz <Digvijo...@gma il.comwrote:
This is a very simplified version of something I am trying to
understand.
The State object holds the strings and maps , and I pass a reference
to State to the process Function which manipulates it based on the tag
passed in.
State is not an object, its a type.
>
I cannot pass the strings/maps in question as references coz ..
a) thats redundant, state has everything, it increases number of
parameters unnecessarily
b) I have to do the case/switch outside the function to decide what to
pass in.( and this is a simple example )

Is it a good idea to use things like string* and std::map..* as i have
tried below
Nothing wrong with storing pointers. You are asking us if the code is
correct and we can't see how you are storing the pointers nor how you
are using the process function. Personally, i'ld have declared an
abstract State class with 2 derivatives, one for each type (instead of
confusing the setup with one variant). The more you try to confuse the
program by using tags to suggest an object is type A or type B, the
less the compiler can do its job: typechecking.

Its rather difficult to comment on anything else. Except to say that
the code looks rather confused to me.
>
Thanks
Digz

------------------------
#include<string >
#include<map>

using namespace std;
typedef std::map<int, stringmapType;

struct State {
string pId_;
mapType pTypeNSymbol_;
string uId_;
mapType uTypeNSymbol_;

};

void process( State& state, int Tag){
typedef mapType::iterat or mItr ;
string* prodId;
mapType* typeSymMap;

switch( Tag ) {
case 0:
prodId = &state.pId_;
typeSymMap = &state.pTypeNSy mbol_;
break;
case 1:
prodId = &state.uId_;
typeSymMap = &state.uTypeNSy mbol_;
break;
}
if (prodId->empty()) {
mItr it;
mItr end = typeSymMap->end();
if ( (it = typeSymMap->find(6)) != end){
*prodId = (*typeSymMap)[6];
}
}

if (!typeSymMap->empty()) {
mItr it = typeSymMap->begin();
*prodId = it->second;
}

}
Jan 8 '08 #2
digz <Di********@gma il.comwrote:
This is a very simplified version of something I am trying to
understand.
The State object holds the strings and maps , and I pass a reference
to State to the process Function which manipulates it based on the tag
passed in.

I cannot pass the strings/maps in question as references coz ..
a) thats redundant, state has everything, it increases number of
parameters unnecessarily
b) I have to do the case/switch outside the function to decide what to
pass in.( and this is a simple example )

Is it a good idea to use things like string* and std::map..* as i have
tried below
I'd like to see how else this State object is used before commenting too
much. However the first thing I would do is break the function up a bit.

void processA( string& prodId, mapType& typeSymMap )
{
// why so short? See notes below...
if ( ! typeSyMap.empty () )
prodId = typeSymMap.begi n()->second;
}

void process( State& state, int tag )
{
switch ( tag ) {
case 0:
processA( state.pId_, state.pTypeNSym bol_ );
case 1:
processA( state.uId_, state.uTypeNSym bol_ );
default:
assert( false && "Bad tag value" );
}
}

Still doesn't make much sense, but I don't know the problem domain.
Comments below:
#include<string >
#include<map>

using namespace std;
typedef std::map<int, stringmapType;

struct State {
string pId_;
mapType pTypeNSymbol_;
string uId_;
mapType uTypeNSymbol_;
};
Can you come up with some better names? Can you come up with some member
functions? The 'process' function below would be an ideal
member-function.
void process( State& state, int Tag){
typedef mapType::iterat or mItr ;
string* prodId;
mapType* typeSymMap;
Initialize your variables at the point of definition.
switch( Tag ) {
case 0:
prodId = &state.pId_;
typeSymMap = &state.pTypeNSy mbol_;
break;
case 1:
prodId = &state.uId_;
typeSymMap = &state.uTypeNSy mbol_;
break;
What if Tag is neither 0 or 1? If it can only be 0 or 1, then why not a
bool?
}
if (prodId->empty()) {
mItr it;
Again, initialize your variables at the point of definition. However,
this variable "it" is completely pointless so remove it.
mItr end = typeSymMap->end();
Remove "end" as well. Extraneous variables cause headaches.
if ( (it = typeSymMap->find(6)) != end){
*prodId = (*typeSymMap)[6];
}
}

if (!typeSymMap->empty()) {
mItr it = typeSymMap->begin();
Another extraneous variable. At least this one is initialized...
*prodId = it->second;
}
}
For the two 'if' statements above... If typeSymMap.empt y() == false,
then the final result will be *prodId == typeSymMap->begin()->second. If
typeSymMap.empt y() == true then there won't be a key of '6' in it so the
first 'if' statement (where it tries to find a key of '6') is utterly
pointless.
Jan 8 '08 #3

"digz" <Di********@gma il.comwrote in message
news:81******** *************** ***********@f47 g2000hsd.google groups.com...
This is a very simplified version of something I am trying to
understand.
The State object holds the strings and maps , and I pass a reference
to State to the process Function which manipulates it based on the tag
passed in.

I cannot pass the strings/maps in question as references coz ..
a) thats redundant, state has everything, it increases number of
parameters unnecessarily
b) I have to do the case/switch outside the function to decide what to
pass in.( and this is a simple example )

Is it a good idea to use things like string* and std::map..* as i have
tried below
Where do you "pass in" any strings/maps?

Also, what's "redundant" about a reference?

And what function are you doing the switch/case "outside of"?

One reason to use pointers in the way you have is because you have to
initialize a local reference at the point of declaration. You can't declare
the reference in one place but initialize it via two different paths (as in
a switch statement). Pointers, on the other hand, can be initialized
wherever you please. Using pointers like you have is fine, except that
there are probably better overall designs which would remove the need to use
pointers.

One idea, and probably the best, might be to use member functions and
polymorphism instead of switch statements.

Another might be to initialize local references (or pointers) via a call to
another function, thus avoiding the problem of trying to have two different
paths of initialization for a local reference. (Of course, that really just
pushes the problem out of the process function and into a different
function. But it clears things up locally, at least. :-))

-Howard

Jan 9 '08 #4

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

Similar topics

3
2607
by: lallous | last post by:
Hello, I have: std::map<std::string, std::string> m; std::string s1; char *p = "hello world", *p1 = "value"; (a) s1 = p; is (a) okay if I later set "p = NULL" ? s1 would have its own copy of the
8
3860
by: Kin Pang | last post by:
Hi, I have a routine where I'm using a std::map from a pair of ints to double for caching evaluation results. The cache depth could be upto say 1000 in depth. However, my application needs to clear and replenish the cache repeatedly and profiling seems to suggests the performance is handicated by repeated calls to new. Can anybody advice...
5
8725
by: Peter Jansson | last post by:
Hello, I have the following code: std::map<int,std::set<std::string> > k; k="1234567890"; k="2345678901"; //... std::set<std::string> myMethod(std::map<int,std::set<std::string> > k) throw(std::runtime_error)
19
6119
by: Erik Wikström | last post by:
First of all, forgive me if this is the wrong place to ask this question, if it's a stupid question (it's my second week with C++), or if this is answered some place else (I've searched but not found anything). Here's the problem, I have two sets of files, the name of a file contains a number which is unique for each set but it's possible...
3
2327
by: Mathieu Malaterre | last post by:
Hello, I have currently a piece a code which look like this: foo.h ------------------- extern const std::string NotFound; foo.cxx
3
3697
by: Dan Trowbridge | last post by:
Hi everyone, In my attempt to port code from VS 6.0 to VS.NET I had some code break along the way, mostly due to not adhereing closely to the C++ standard. This may be another instance but I can't think of a good fix, or even why it broke. The problem In one of my CFormView derived classes I have a member variable of the type...
13
1423
by: Peteroid | last post by:
Why does reading a member of a std::map not considered const? For example: class My_Class { int Get_Map_Value( int index ) const // ** error ** not considered const!!! { return m_Map ; // note that m_Map is not changed, only read from }
1
3701
by: Maxwell | last post by:
Hello, I having having oodles of trouble using the std lib in my MC++ (VS.NET 2003) Class library. I figured out a simple sample to reproduce the errors I am having. Create a MC++ (VS.NET 2003) class library and type in the following code below: #include <map> #include<string>
2
2313
by: darkkal | last post by:
Hi I'm a student majoring a computer . I tried to make a simple hashing table like (int) 1 , (std::string) "One" There is a problem in using std::map ㅜㅜ this is a simple code. PLZ~~Help me :) #include <iostream>
0
7426
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7368
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7610
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7774
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
5299
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
4920
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3418
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3412
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
989
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.