|
Hey, I'm not fighting.
--
You're entertaining at least.
|
|
|
|
|
The code example provided by Jörgen Sigvardsson was correctly updating a map of ints. It is easy to change this example to work with strings.
<br />
#include <map><br />
#include <iostream><br />
#include <utility><br />
#include <algorithm><br />
#include <string><br />
<br />
<br />
typedef std::map<std::string,std::string> StringMap;<br />
<br />
struct update<br />
{<br />
void operator() ( StringMap::value_type& v ) const<br />
{<br />
if ( v.first == "second" )<br />
v.second = "replaced value";<br />
}<br />
};<br />
<br />
struct print<br />
{<br />
void operator() ( StringMap::value_type& v ) const<br />
{<br />
std::cout << "first = " << v.first << ", second = " << v.second << std::endl;<br />
}<br />
};<br />
<br />
int main( int argc, char* argv[] )<br />
{<br />
StringMap testMap;<br />
<br />
testMap.insert( StringMap::value_type("first", "1") );<br />
testMap.insert( StringMap::value_type("second", "2") );<br />
testMap.insert( StringMap::value_type("third", "3") );<br />
testMap.insert( StringMap::value_type("fourth", "4") );<br />
testMap.insert( StringMap::value_type("fifth", "5") );<br />
<br />
std::for_each(testMap.begin(), testMap.end(), print());<br />
std::for_each(testMap.begin(), testMap.end(), update());<br />
std::for_each(testMap.begin(), testMap.end(), print());<br />
<br />
return 0;<br />
}<br />
However, as you know (from your comments in your original post), the code in the "update" functor is basically doing a find. However, since the for_each does a loop through all elements of the map, this is much less efficient than doing an actual find. If you need to do single updates to elements in a map which have particular key values, it would normally be better to just do the find and then update the element. The for_each algorithm is really only appropriate if you need to access every element in the container.
Best regards,
John
|
|
|
|
|
Thanks for replying.
Your sample does work.
William
Fortes in fide et opere!
|
|
|
|
|
jbarton wrote:
If you need to do single updates to elements in a map which have particular key values, it would normally be better to just do the find and then update the element.
That depends a little on how many values you are updating. If you are updating few elements, then the approach you mention is more efficient. But if the number of values are many, then an iteration is faster. Assuming that the map is implemented as a balanced search tree, you need to find out how O(k * log(n)) relates to O(n), where k is the number of values to update, and n is the number of elements in the map. Of course, if it's a hashmap, then the approach you mention is most likely faster for basically all k (unless, of course, the hash algorithm is very slow).
I guess it would be advantageous to encapsulate this algorithm into its own function, and let itself adapt to the problem size automatically. But hey, how would I know? I just post faulty samples and is a retard according to some.
--
You're entertaining at least.
|
|
|
|
|
I think the idea of iterating through the map to update multiple elements has merit. If (for instance) I want to update the data associated with all keys that match a particular pattern, there really isn't any way that I could do it without iterating through all elements in the map.
When I have a list of specific key values whose data I want to update, I find it difficult to set up a functor that efficiently checks for these key values and then updates the associated data values. It may be possible using another map or hash within the functor to lookup the key value and then get the replacement value. With only simple if / else if comparisons, I think that the overhead of the functor would probably remove any advantage gained by not using find on each key value.
Unless the program I was writing was running too slowly and by profiling I found it to be due to using find to update the map, I would probably stick with using find.
If you know of a better way of writing this functor, let me know. I am always interested in hearing other approaches.
Best regards,
John
|
|
|
|
|
Hello,
please tell me how to make a 2d and 3d vector using stl....and also how to assign and get values from it then.....i think i have made the 2d vector correctly but cant seem to assign values to it..THIS IS THE APPROACH I USED
typedef std::vector<int> sArray;
std::vector<sarray> m_map;
std::vector<int> v;
v.resize(m_no_horz_tiles,-1);
for (int i = 0 ; i < m_no_vert_tiles; i++)
{
m_map.push_back(v);
}
but the thing is i cant assign values to my 2d vector like this, given i and j
m_map[i][j] = 1; //value not stored don know why ?????
please guide me...
Thank you
|
|
|
|
|
Interesting.
Try initializing it via constructor.
std::vector v(m_no_horz_tiles, 0);
Kuphryn
|
|
|
|
|
Hello Omar,
I rewrote your code above as follows :
#include <vector>
#include <iostream.h>
#include <stdio.h>
typedef std::vector<int> sArray;
void main()
{
std::vector<sArray> m_map;
std::vector<int> v;
int i = 0, j = 0, m_no_horz_tiles = 5, m_no_vert_tiles = 10;
v.resize(m_no_vert_tiles, 0);
for (i = 0 ; i < m_no_horz_tiles; i++)
{
m_map.push_back(v);
}
for (i = 0 ; i < m_no_horz_tiles; i++)
{
for (j = 0 ; j < m_no_vert_tiles; j++)
{
m_map[i][j] = (i * 10) + j;
}
}
for (i = 0 ; i < m_no_horz_tiles; i++)
{
for (j = 0 ; j < m_no_vert_tiles; j++)
{
cout << "m_map[" << i << "][" << j << "] == " << m_map[i][j] << endl;
}
}
}
The above code yielded the following output :
m_map[0][0] == 0
m_map[0][1] == 1
m_map[0][2] == 2
m_map[0][3] == 3
m_map[0][4] == 4
m_map[0][5] == 5
m_map[0][6] == 6
m_map[0][7] == 7
m_map[0][8] == 8
m_map[0][9] == 9
m_map[1][0] == 10
m_map[1][1] == 11
m_map[1][2] == 12
m_map[1][3] == 13
m_map[1][4] == 14
m_map[1][5] == 15
m_map[1][6] == 16
m_map[1][7] == 17
m_map[1][8] == 18
m_map[1][9] == 19
m_map[2][0] == 20
m_map[2][1] == 21
m_map[2][2] == 22
m_map[2][3] == 23
m_map[2][4] == 24
m_map[2][5] == 25
m_map[2][6] == 26
m_map[2][7] == 27
m_map[2][8] == 28
m_map[2][9] == 29
m_map[3][0] == 30
m_map[3][1] == 31
m_map[3][2] == 32
m_map[3][3] == 33
m_map[3][4] == 34
m_map[3][5] == 35
m_map[3][6] == 36
m_map[3][7] == 37
m_map[3][8] == 38
m_map[3][9] == 39
m_map[4][0] == 40
m_map[4][1] == 41
m_map[4][2] == 42
m_map[4][3] == 43
m_map[4][4] == 44
m_map[4][5] == 45
m_map[4][6] == 46
m_map[4][7] == 47
m_map[4][8] == 48
m_map[4][9] == 49
Hope the above helps.
Thanks,
Bio.
|
|
|
|
|
hi...I am new in the field of ATL...Please suggest some books which take the reader from Beginning to programming stage...
thanks...
utkarsh sharma
"Not everything that counts can be counted, and not everything that can be counted counts."
|
|
|
|
|
The best ATL book so far I've read is ATL Internals[^], by Brent Rector and Chris Sells.
That book together with Essential COM[^] by Don Box is a perfect combo for learning COM. If you're new to COM, I'd suggest you start with Essential COM.
--
You're entertaining at least.
|
|
|
|
|
thanks..for the names ATL Internal and Essential COM...
utkarsh sharma
"Not everything that counts can be counted, and not everything that can be counted counts."
|
|
|
|
|
Hi,,
I make an activeX Control using ATL
I added a full control with the font stock property..
now i want to get notified when this property changes..(after the user changes the property i want to perform some action)...
how can i do this ?
|
|
|
|
|
Take a look at the CStockPropImpl source code (atlctl.h). There you will find code like this:
HRESULT STDMETHODCALLTYPE put_Font(IFontDisp* pFont)
{
...
__if_exists(T::OnFontChanged)
{
pT->OnFontChanged();
}
...
} So, for the property Font you add the OnFontChanged function. For the property Picture, you add OnPictureChanged, etc.
--
They say the most horrible things, but I hear violins.
When I close my eyes, I'm at the center of the sun.
|
|
|
|
|
thank you for the reply but i revised put_Font and it does not contain that code ..
Do you use ATL 7.0 ?
I use ATL 3.0 with VC++6 and I need to implement this functionaliy..
Can you help with this ?
maybe by implementing the OnChange(DISPID) function of IPropertyNotifySink..
Can you help me with this ?
|
|
|
|
|
Sorry, I don't have/use ATL3, so I don't have a fast solution for you.
However, you can always override put_Font et al, and chain the call back to CStockPropImpl<...>::put_Font. If the chain call returns S_OK, then you know the font has been set, and you can perform your specific action.
--
They say the most horrible things, but I hear violins.
When I close my eyes, I'm at the center of the sun.
|
|
|
|
|
thanks again
seems this will be the solution till i get the idea .. or till i use ATL 7.0
|
|
|
|
|
I have the code exactly like the sample OnlineAddressBook from the MSDN library yet I get the error whereas that app does not. Such simple things can stop a programmer for hours.
Here is the code I am using. Note that the Web Service this code is in compiles fine, it's when I compile the SRF (ATL Server) project that the error occurs, let me know if you need to see more code.
// DataConsumerWS.h : Defines the ATL Server request handler class
//
#pragma once
namespace DataConsumerWSService
{
const wchar_t MYDATASOURCE[] = L"Provider=SQLOLEDB.1ersist Security Info=False;User ID=me;Initial Catalog=mine;Data Source=SERVER;Use Procedure for Prepare=1;Auto Translate=Trueacket Size=4096;Use Encryption for Data=False;Tag with column collation when possible=False";
// all struct, enum, and typedefs for your webservice should go inside the namespace
// IDataConsumerWSService - web service interface declaration
//
[
uuid("2E179D71-E436-42EE-A1D6-00758BF3D7AA"),
object
]
__interface IDataConsumerWSService
{
// HelloWorld is a sample ATL Server web service method. It shows how to
// declare a web service method and its in-parameters and out-parameters
[id(1)] HRESULT GetStudents([out] int *arrSize,[out,size_is(*arrSize)] BSTR **NameList,[out,size_is(*arrSize)] LONG **IDList);
// TODO: Add additional web service methods here
};
// DataConsumerWSService - web service implementation
//
[
request_handler(name="Default", sdl="GenDataConsumerWSWSDL"),
soap_handler(
name="DataConsumerWSService",
namespace="urn:DataConsumerWSService",
protocol="soap"
)
]
class CDataConsumerWSService :
public IDataConsumerWSService
{
public:
// uncomment the service declaration(s) if you want to use
// a service that was generated with your ISAPI extension
CDataConnection m_dc;
HTTP_CODE InitializeHandler(AtlServerRequest *pRequestInfo, IServiceProvider *pProvider)
{
if (HTTP_SUCCESS != CSoapHandler::InitializeHandler(pRequestInfo, pProvider))
return HTTP_FAIL;
|
|
|
|
|
Never mind. The problem with using MSDN samples is not being clear on Wizard options selected. Anyway, as I was perusing the process of running sproxy.exe for Console App linkage to ATL Web Services, I came across the .wsdl file that needed to be generated. .NET Studio does this under the Add Rerferences utility, however you first have to open the .disco file to get the http path to your web service. Studio then generates the XML document to link your client to the soap handlers ( .wsdl file). Tis done and tis works.
Whew!
|
|
|
|
|
Hello,
I have a appWizard generated ATL project "with MFC support" (VC++ 6.0 SP5). I try to use Project->Add to Project->Files to add a MFC class (.cpp & .h files). The class shows up in the class view but Class Wizard does not recognize the existance of the new class.
Am I doing something wrong? Any suggestions or different ways to do this. Using Add "New" class works, but I need to use an existing class from files, I do not what to have to recreate a bunch of classes from scratch.
Thanks,
Joe Colosi
|
|
|
|
|
I have a stored procedure that returns more than one result set (mulitiple select statements). I am using ATL Server and have found an example from Microsoft called OnlineAddressBook. This example has helped me considerably with the OLEDB Consumer Template, specifically db_command and db_column. However, all though it indicates the possibility of moving thru more than one result set it doesn't give any clear example, such as syntax or whether or not binding can be manual or has to be automatic. I am sure this is a simple process but it is eluding me and I cannot find any examples or user posts anywhere on this.
I have:
[
db_command(L"{CALL dbo.spTranscriptGetStudents(?, ?, ?) }")
]
class CGetStudents
{
public:
// In order to fix several issues with some providers, the code below may bind
// columns in a different order than reported by the provider
[ db_column(1) ] LONG m_StudentNum;
[ db_column(2) ] TCHAR m_LastName[DB_MAX_FIELDLEN + 1];
[ db_column(3) ] TCHAR m_FirstName[DB_MAX_FIELDLEN + 1];
[ db_param(1) ] DBTIMESTAMP m_asofdate;
[ db_param(2) ] LONG m_schnum;
[ db_param(3) ] LONG m_schyear;
};
|
|
|
|
|
I think I solved this one also. At least it compiled, have to run it to test that it works. Basically bind the columns of the first result set to the db_command using db_column. Then in the receiving method check to see if you can .MoveNext record in the first result set if not use the .GetNextResult method to move to the next one.
Heres the code:
while (S_OK!=GetStudents.MoveFirst())
{
GetStudents.GetNextResult(&numRecs, true);
}
|
|
|
|
|
I'd like to know how to host a windowless ActiveX control in WTL, a Flash control in particular.
In MFC, I add it to the dialog via the resource editor, change the control's window property to transparent and when I run the program I see in the debug output that 'Control wants to be windowless'.
And indeed, the flash control is completely transparent.
But when I follow the same steps in WTL, I can't get the control to lose its window.
Thanks
|
|
|
|
|
You always get a window that hosts the window less control. It might be the case that in MFC, the dialog itself acts as a host for a window less control. Not sure about that.
But if you use the CAxWindow class in ATL (suitable for WTL), it'll contain one window for window less controls (the hosting window) and two windows for windowed controls (one window for hosting the control, and one window which is the control itself).
I'd advise you to use CAxWindow. It's fast and easy. Be aware of problems however if you are using WTL 3.x/7.0 with ATL7. WTL 3.x/7.0 uses the old ATL3 way of dealing with CComModules, which can be a hassle if you don't know the "tricks" (assertions firing all the time )
--
Watcha' gonna do, when Hulkamania runs wild on you!?
|
|
|
|
|
Thanks for the reply, but I still have no idea what I have to do
Are you suggesting the problem is with my base class, CAxDialogImpl? Or should I subclass the control's window in a certain way?
Thanks
BTW, I'm using WTL 7.1.
|
|
|
|
|
I just had a peek at the implementation of CAxDialogImpl. I saw that it uses CAxWindow (and CAxWindow2) for hosting the actual controls. It does not host the controls directly, hence you'll always get at least one window per activex control.
There's not much you can do, unless you implement your own dialog which hosts the controls directly (tricky business).
But what's so wrong with an extra window? Your control will function properly!
--
Watcha' gonna do, when Hulkamania runs wild on you!?
|
|
|
|
|