473,666 Members | 2,010 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 2057
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
2614
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
3876
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 me on how to improve the performance or suggests alternatives please.
5
8734
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
6137
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 (even probable) that two files in different sets have the same numbers. I want to store these...
3
2335
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
3708
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
1435
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
3719
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
2322
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
8445
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 usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8356
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 synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8871
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. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8781
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 captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8551
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
5664
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 into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4369
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2011
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1776
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.