|
May I know how I can access map by index just like I access vector by index?
cout<< "This is the 10th element (0 based) in my vector = "<< myVector[10];
For map, when I wish to access the 10th element (0 based), I would do the following:
map<char *,="" int="">::iterator curr, end;
end = myMap.end();
curr = myMap.begin();
for(int i=0; i<10; i++) {
curr++;
}
cout<< "This is the 10th element (0 based) in my map = "<< myMap->second;
I was wondering whether is there any better way other than having to loop through all the items? For example:
/* The following code is just a wish, it is not valid. */
cout<< "This is the 10th element (0 based) in my vector = "<< myMap.getItemByIndex(10);
Thank you.
cheok
|
|
|
|
|
std::map is not a random access container, you cannot access a item directly like you can do for std::vector.
Me think that accessing an indexed element of a map ( i.e. the 10th element ) is not usefull.
AFAIK, looping maps like you do will work.
Maximilien Lincourt
Your Head A Splode - Strong Bad
|
|
|
|
|
You can but there could be a high price to pay performance-wise depending our the std::map implementation you are using:
#include <map>
#include <string>
#include <iostream>
void main( )
{
typedef std::map<int, std::string> Numbers;
Numbers n;
n.insert(Numbers::value_type(1,"one"));
n.insert(Numbers::value_type(2,"two"));
n.insert(Numbers::value_type(3,"three"));
Numbers::iterator iCurrent = n.begin();
// You can use the 0-based index of the element to access
// This is not an efficient way to access element of map
// and there is no out-of-boundary validation. You have
// been warned!
std::advance(iCurrent, 1);
std::cout << (iCurrent->second).c_str() << std::endl;
}
A better approach would probably be to store your data in a std::vector, the sort it with std::sort. This way you can use std::find to achieve logarithmic access time as per std::map or instant access time with std::vector's operator[].
Good luck,
g.
|
|
|
|
|
Hello,
the std::map container is not designed for array indexed access. If you need this, you probably chose the wrong container.
You can however copy the contents of the map to a vector and access it from there:
std::vector<int> vec;
vec.insert(yourmap.begin(), yourmap.end());
int i = vec[1];
Behind every great black man...
... is the police. - Conspiracy brother
Blog[^]
|
|
|
|
|
Itr to edit a record of a table which it has fields of type float.
When these fields have decimals the update cannot be done. But when they are without decimals the update is successful.
Does anyone know how can I update the record even if it has fields with decimals?
Thanks in Advance
|
|
|
|
|
Without seeing a relevant code snippet, it's hard to say what you are doing wrong.
"One must learn from the bite of the fire to leave it alone." - Native American Proverb
|
|
|
|
|
double value=15.1
TableSet->Open();
TableSet->MoveFirst();
while(!TableSet->IsEOF()) {
if(TableSet->m_field1 == id) {
TableSet->Edit();
TableSet->m_field2 += value;
TableSet->Update();
} else {
TableSet->MoveNext();
}
}
When TableSet->m_field2, which is of type float, has decimals the update return error.
When it does not have decimals no error occurs.
Any idea?
|
|
|
|
|
sirtimid wrote:
...the update return error.
And what would that error be?
What does the DoFieldExchange() method look like for the CRecordset -derived class?
Is the corresponding field in the database table a floating-point type?
"One must learn from the bite of the fire to leave it alone." - Native American Proverb
|
|
|
|
|
Is it correct in anyway to declare a same variable (variable type and name same) multiple times in Visual C++ code of single block ?
For example,
void Function( int i, int j )
{
HRESULT hr = E_FAIL;
.
.
.
HRESULT hr = CComObject::GetInterface(...);
.
.
}
Appreciate answers and suggestions. Thanks in advance.
|
|
|
|
|
No you can't. Otherwise the compiler has no clue of which instance you are working with.
-- modified at 10:19 Thursday 8th September, 2005
Seing your post, a thing what you can do is simply not redifine your variable:
HRESULT hRes = E_FAIL;<br />
....<br />
<br />
hRes = ...<br />
|
|
|
|
|
Is there any facility in Visual C++ to satisfy the compiler by specifying compile (or build) options or MACRO definitions to accept redeclaration of variable of similar type in same block ?
|
|
|
|
|
Why would you want ot redeclare the same variable in the same logical code block ?
can't you declare it at the top of the block, and assign ( and re-assign ) values as needed ?
...
int iValue;
...
iValue = 4;
...
iValue = 10;
...
maybe your variable type does not support an = operator ( if it's a class ), you should define one, or do something like :
MyClass* pMyClass;
pMyClass = new MyClass( 5 );
...
delete pMyClass;
pMyClass = new MyClass( 10 );
...
delete pMyClass;
Maximilien Lincourt
Your Head A Splode - Strong Bad
|
|
|
|
|
It would only be syntactically correct if you did it thusly:
void Function( int i, int j )
{
HRESULT hr = E_FAIL;
.
.
{
HRESULT hr = CComObject::GetInterface(...);
}
.
.
}
"One must learn from the bite of the fire to leave it alone." - Native American Proverb
|
|
|
|
|
Hi, i am using multithreading in my application. I want to use MFC object(view and MainFrame object) in my worker thread. Can anybody tell how to do this.
Thanks.
|
|
|
|
|
What do you mean by "use?" Worker threads (i.e., those without a message pump) should not interact with UI components other than to post a message to the component's owner. The owner is therefore responsible for updating the UI component.
"One must learn from the bite of the fire to leave it alone." - Native American Proverb
|
|
|
|
|
From within your worker thread you have a handle to its parent. Using this handle you'll be able to use/interact with this MFC object.
Marc Soleda.
... she said you are the perfect stranger she said baby let's keep it like this... Tunnel of Love, Dire Straits.
-- modified at 10:46 Thursday 8th September, 2005
|
|
|
|
|
No, you don't want to use your view and mainframe objects in a worker thread. That will cause a disaster of biblical proportions. Those objects belong to the GUI thread.
The way your work thread will interact with the GUI is by passing the thread a pointer to the view or mainframe window, and then posting messages to this window.
Go to flounder.com and read Dr Newcomber's articles on threading.
|
|
|
|
|
ashtwin wrote:
. I want to use MFC object(view and MainFrame object) in my worker thread. Can anybody tell how to do this.
you will find this article intresting -
http://www.codeproject.com/threads/usingworkerthreads.asp[^]
"Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow
cheers,
Alok Gupta
VC Forum Q&A :- I/ IV
|
|
|
|
|
I open the table and I search in it row by row as follows:
TableSet->Open();
TableSet->MoveFirst();
while(!TableSet->IsEOF()) {
if(TableSet->m_field1 == id) {
TableSet->Edit();
TableSet->m_field2 += 10;
TableSet->Update();
} else {
TableSet->MoveNext();
}
}
The problem is that when the program goes in the if statement (the row has been found), and it is the first row of the table, the update operation returns:
'No rows where affected by the update or delete operation'
If this is not the first row the update operation success.
Does anyone know why does this happen?
Thanks in advance
sirtimid
|
|
|
|
|
This seems very inefficient. Rather than iterate through each row of the table, why not use a WHERE clause so that the recordset only contains the row(s) that need(s) updating?
"One must learn from the bite of the fire to leave it alone." - Native American Proverb
|
|
|
|
|
I have the need within my application to access a set of files, much like the MRU file option.
So what I need to do is store an integer and several strings in the registry, and retrive them when a particular dialog is displayed.
Can anyone point me in the right direction for some sample code as I have never used the registry before.
I will be deploying the aplication so can I test it within Visual Studio IDE and again when deployed.
Many thanks,
|
|
|
|
|
Search for the word registry in the search box, just below "THE CODE PROJECT" at the very top of page.
There are at least 10 different articles (in the VC++ section) here at CodeProject. You will find everything you need, I'm sure.
Good luck.
|
|
|
|
|
There are several registry articles inside codeproject that may explain the registry[^].
A good article is this one[^] that is a class that makes it easier.
Marc Soleda.
... she said you are the perfect stranger she said baby let's keep it like this... Tunnel of Love, Dire Straits.
|
|
|
|
|
If you are using MFC, check out the CWinApp::LoadStdProfileSettings() method. You can either use it directly, or look at how it is coded and do something similar. In a key named Recent File List, it creates REG_SZ values named File# where # is a number starting at 1. Those values are then assigned the absolute path of a file.
"One must learn from the bite of the fire to leave it alone." - Native American Proverb
|
|
|
|
|
Can anyone tell me where WITH_ZLIB is hidden in my visual studio 7 project? I know it is being used but I can’t find where it is in order to turn it off.
There is a CMakeLists.txt file, cut and paste from which shows:
# openssl support
OPTION(WITH_OPENSSL "Turn support for openssl on/off" OFF)
FIND_PATH(WITH_OPENSSLINC "include/openssl/ssl.h" ${dcmtk_SOURCE_DIR}/../openssl-0.9.7d)
# zlib support
OPTION(WITH_ZLIB "Turn support for zlib on/off" OFF)
FIND_PATH(WITH_ZLIBINC "include/zlib.h" ${dcmtk_SOURCE_DIR}/../zlib-1.2.1)
(The WITH_ZLIB was turned ON, but I changed it to OFF and this had no effect.)
I know it is being used because I see it in BuildLog.htm, (see below “WITH_ZLIB”):
Creating temporary file "c:\Ilan\dcmtk-3.5.3\dcmdata\libsrc\Debug\RSP000001.rsp" with contents
[
/Od /I "../../config\include" /I "../../dcmdata\include" /I "../../ofstd\include" /I "../../../zlib-1.2.1\include" /D "_DEBUG" /D "WIN32" /D "_LIB" /D "_REENTRANT" /D "WITH_LIBPNG" /D "WITH_LIBTIFF" /D "WITH_ZLIB" /D "dcmdata_EXPORTS" /D "CMAKE_INTDIR=\"Debug\"" /D "_MBCS" /FD /EHsc /RTC1 /MTd /GS /YX"stdafx.h" /Fp".\Debug/dcmdata.pch" /Fo".\Debug/dcxfer1.obj" /Fd".\Debug/" /W3 /c /Z7
".\dcxfer.cc"
]
Creating command line "cl.exe @"c:\Ilan\dcmtk-3.5.3\dcmdata\libsrc\Debug\RSP000001.rsp" /nologo"
I looked at the Properties window and the Options, but I simply can’t find who is defining this variable.
Can anyone tell me what I’m missing?
Thanks,
Ilan
|
|
|
|