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

can I use STL on visual C++ type?

I'm trying to make a map to store the static information of pairs of
string and hkey. So here is the code I ahve so far which DOES NOT
compile. Since I am very new to STL, I do not know how to fix it.

typedef std::map<LPCWSTR,HKEYMapType;
typedef MapType::value_type ValuePair;
MapType keyMap

keyMap.insert(ValuePair("HKCR",HKEY_CLASSES_ROOT)) ;
keyMap.insert(ValuePair("HKLM",HKEY_LOCAL_MACHINE) );
keyMap.insert(ValuePair("HKCR",HKEY_CURRENT_USER)) ;
keyMap.insert(ValuePair("HKU",HKEY_USERS));
keyMap.insert(ValuePair("HKDD",HKEY_DYN_DATA));

so that later on I can use it as this:
WalkRegistry("HKLM","Software\\ActiveState");
The WalkRegistry has body of follows:
HRESULT WalkRegistry(LPCWSTR rootKey,LPCWSTR subKey){
HRESULT ret=S_FAIL;
HKEY hSubkey=NULL;
if(ERROR_SUCCESS==::RegOpenKeyEx(rootKey,subKeyNUL L,KEY_READ,&hSubkey))
{
::RegCloseKey(hSubkey);
} else {
std::cout<<"error"<<std::endl;
};

return ret;
}

Can I use STL this way?
Thanks

Aug 30 '06 #1
3 3292
Yes you can use it, but you need to follow STL rules as well as Win32 SDK
rules.

learning wrote:
I'm trying to make a map to store the static information of pairs of
string and hkey. So here is the code I ahve so far which DOES NOT
compile. Since I am very new to STL, I do not know how to fix it.

typedef std::map<LPCWSTR,HKEYMapType;
Stop right there. LPCWSTR is a wide string. Do you really want a
std::wstring there?

Or do you need a std::string there, and is the W a typo?

Next, map needs to be able to compare its keys. LPCWSTR is like char *,
where simply compares the memory address of two pointers (IIRC).

You need a lexical comparison, so two "HKCR" keys get interpreted as the
same key (not two different memory locations). So you need no pointers, you
need real string objects:

std::string;
std::wstring;
CString;
CStringW;

Next, the exact behavior of CString and its ... ilk ... is off-topic for
this newsgroup. An MS-specific group will be better qualified to tell if I
got any of these details wrong.

Next...
keyMap.insert(ValuePair("HKCR",HKEY_CLASSES_ROOT)) ;
One typically populates a map by assignment:

keyMap[L"HKCR"] = HKEY_CLASSES_ROOT.

Next, your computer already "knows" that HKCR is an alias for
HKEY_CLASSES_ROOT. Why are you telling it redundant information? (Answer on
an on-topic newsgroup! ;-)
WalkRegistry("HKLM","Software\\ActiveState");
Have you softened this topic with a search on groups like codeproject.com?
There are lots of cute Registry wrappers out there...

--
Phlip
http://c2.com/cgi/wiki?ZeekLand <-- NOT a blog!!!
Aug 30 '06 #2
learning wrote:
I'm trying to make a map to store the static information of pairs of
string and hkey. So here is the code I ahve so far which DOES NOT
compile.
Does not compile and says WHAT?
Since I am very new to STL, I do not know how to fix it.
What you posted has plenty of non-standard things that are off-topic
here. If you want us to be able to help you, post the error messages.
Another possibility for you is to post in 'microsoft.public.vc.language'
or 'microsoft.public.vc.mfc', where all those MS-specific types and
constructs are on topic.
typedef std::map<LPCWSTR,HKEYMapType;
typedef MapType::value_type ValuePair;
MapType keyMap

keyMap.insert(ValuePair("HKCR",HKEY_CLASSES_ROOT)) ;
keyMap.insert(ValuePair("HKLM",HKEY_LOCAL_MACHINE) );
keyMap.insert(ValuePair("HKCR",HKEY_CURRENT_USER)) ;
keyMap.insert(ValuePair("HKU",HKEY_USERS));
keyMap.insert(ValuePair("HKDD",HKEY_DYN_DATA));

so that later on I can use it as this:
WalkRegistry("HKLM","Software\\ActiveState");
I don't see any 'keyMap' used here. Why do you need it?
The WalkRegistry has body of follows:
HRESULT WalkRegistry(LPCWSTR rootKey,LPCWSTR subKey){
HRESULT ret=S_FAIL;
HKEY hSubkey=NULL;
if(ERROR_SUCCESS==::RegOpenKeyEx(rootKey,subKeyNUL L,KEY_READ,&hSubkey))
{
>>RegCloseKey(hSubkey);
} else {
std::cout<<"error"<<std::endl;
};

return ret;
}

Can I use STL this way?
Which way? I only see a bunch of 'insert' calls. Did you mean to
look your values up somehow?

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Aug 30 '06 #3
"Phlip" <ph******@yahoo.comwrote in message
news:O6***************@newssvr25.news.prodigy.net. ..
Yes you can use it, but you need to follow STL rules as well as Win32 SDK
rules.
<snip>
Next...
>keyMap.insert(ValuePair("HKCR",HKEY_CLASSES_ROOT) );

One typically populates a map by assignment:

keyMap[L"HKCR"] = HKEY_CLASSES_ROOT.
The assignment method certainly looks nicer, but using insert seems to be
more efficient (if that matters in a given situation). Here's the code I
used to time it (unfortunately I had to use a non-Standard library to time
it in milliseconds, but it's fairly obvious what's going on):

#include <SDL.h>
#include <iostream>
#include <map>
#include <utility>

std::map<int,inttheMap;

unsigned int time_it(void (*f)())
{
unsigned int start = SDL_GetTicks();
f();
return SDL_GetTicks() - start;
}

void map_array()
{
for(int i=0; i<100000; ++i) theMap[i] = i;
}

void map_insert()
{
for(int i=0; i<100000; ++i) theMap.insert(std::make_pair(i,i));
}

int main(int argc, char *argv[])
{
SDL_Init(SDL_INIT_TIMER);
for(int i=1; i<=10; ++i)
{
std::cout << "TEST #" << i << '\n';
std::cout << "Associative array version: " << time_it(&map_array) <<
" milliseconds\n";
theMap.clear();
std::cout << "Insert version: " << time_it(&map_insert) << "
milliseconds\n\n";
theMap.clear();
}
SDL_Quit();
return 0;
}

The results I got were (condensed to save space):

TEST #1 Associative array version: 2549 milliseconds Insert version: 1977
milliseconds
TEST #2 Associative array version: 2434 milliseconds Insert version: 1959
milliseconds
TEST #3 Associative array version: 2429 milliseconds Insert version: 1959
milliseconds
TEST #4 Associative array version: 2424 milliseconds Insert version: 1953
milliseconds
TEST #5 Associative array version: 2481 milliseconds Insert version: 2070
milliseconds
TEST #6 Associative array version: 2502 milliseconds Insert version: 2039
milliseconds
TEST #7 Associative array version: 2496 milliseconds Insert version: 2137
milliseconds
TEST #8 Associative array version: 2569 milliseconds Insert version: 2083
milliseconds
TEST #9 Associative array version: 2585 milliseconds Insert version: 2013
milliseconds
TEST #10 Associative array version: 2545 milliseconds Insert version: 1994
milliseconds

This doesn't indicate that using insert is always the right way to go, but
it does seem to suggest that if you're adding a large number of key-value
pairs to a map, you might want to prefer insert. Assuming I haven't done
something stupid in my code, it would seem to be about 20% faster (the
associative array version takes roughly 2.5s each time, the insert version
takes about 2s, 2/2.5 roughly equals 0.8...). FWIW, I haven't tried it with
other data types, so for all I know this might just be the case when the key
type is int. YMMV :)

HTH,
Stu
Next, your computer already "knows" that HKCR is an alias for
HKEY_CLASSES_ROOT. Why are you telling it redundant information? (Answer
on an on-topic newsgroup! ;-)
>WalkRegistry("HKLM","Software\\ActiveState");

Have you softened this topic with a search on groups like codeproject.com?
There are lots of cute Registry wrappers out there...

--
Phlip
http://c2.com/cgi/wiki?ZeekLand <-- NOT a blog!!!

Sep 2 '06 #4

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

Similar topics

0
by: Tom Lee | last post by:
Hi, I'm new to .NET 2003 compiler. When I tried to compile my program using DEBUG mode, I got the following errors in the C:\Program Files\Microsoft Visual Studio .NET 2003\Vc7 \include\xdebug...
6
by: jemmaq99 | last post by:
Using Visual Studio .NET 2003 I've tried to add a COM reference to a Windows Form project via the Add Reference dialog (specifically the Active DS Type Library) but always get the error message: ...
19
by: Alf P. Steinbach | last post by:
// As usual the error message directs one to the report the bug. // // And as usual there is absolutely no way to do so without paying for // the privilege... // // Or using three or four hours...
5
by: Anton Noll | last post by:
We are using Visual Studio 2003.NET (C++) for the development of our software in the fields digital signal processing and numerical acoustics. One of our programs was working correctly if we are...
4
by: LCAdeveloper | last post by:
I have had to move to Visual Studio.NET Pro. from Visual Basic 4.0 and am now starting to re-write our code. I was a bit surprised to find that Visual Basic.NET no longer supports fixed length...
11
by: Rolf Welskes | last post by:
Hello, the problem seems to be complex and is in all developments of web-controls which uses own TypeConverter. For this I have here a simple demo-program of the problem: The Control-code: A...
5
by: 2beagles | last post by:
I have a template for a three dimensional array that I have been working on using Visual C++ 6.0. Under version 6 this code worked fine. However, last night I downloaded Visual C++ 2005 Express and...
0
by: =?Utf-8?B?ZGF2aWQ=?= | last post by:
Last week I asked a question about connection to database from client machine (developer machine). There are two types of security setup for SQL Server database: Windows only and "SQL Server and...
0
jwwicks
by: jwwicks | last post by:
Introduction This tutorial describes how to use Visual Studio to create a new C++ program, compile/run a program, resume work on an existing program and debug a program. It is aimed at the...
13
by: miztaken | last post by:
Hi, My C# application have a following code to create an instance of Visual Studio. System.Type type = System.Type.GetTypeFromProgID("VisualStudio.DTE. 8.0"); Object obj =...
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?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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...
0
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
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
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.