|
ekimpl wrote: 'find' isn't of much use in my case, since it only returns first match.
I think that find is very useful in almost all cases (in your case too).
I've modified your FindNumber function and now it works fine.
Here is the commented code:
bool FindNumber(const std::vector<sStringStruct>&v, const std::string& nString)
{
bool found = false;
typedef std::vector<sStringStruct> t_vec;
sStringStruct searched_string = sStringStruct(nString);
t_vec::const_iterator i = std::find(v.begin(), v.end(), searched_string);
while(i != v.end())
{
found = true;
std::cout<<"MATCH:"<<(*i).m_nString << std::endl;
i = std::find(i+1, v.end(), searched_string);
}
return found;
}
Cheers
|
|
|
|
|
Thanks for quick reply. Now that I see Your solution I don't know if I understand the concept of iterators correctly.
I thought that t_vec::const_iterator i = std::find(v.begin(), v.end(), searched_string) would result in i pointing only to the matches, but it is only used to point the first match. And that is why You used recurency to find the next matches.
So if I wanted to know all the indexes of found matches outside of FindNumber, it should return an array of found indexes, yes ?
|
|
|
|
|
ekimpl wrote: Now that I see Your solution I don't know if I understand the concept of iterators correctly.
This[^] is a good place for understanding them.
ekimpl wrote: I thought that t_vec::const_iterator i = std::find(v.begin(), v.end(), searched_string) would result in i pointing only to the matches
In general iterators are used to access elements of a sequence, and can be used in a similar manner to pointers. Consequently an iterator can point only to one concrete element of a sequence at current time, just like a pointer.
ekimpl wrote: So if I wanted to know all the indexes of found matches outside of FindNumber, it should return an array of found indexes, yes ?
Yes, but using indexes is OK only for a std::vector container, because it supports RandomAccessIterator so, you could easily use operator[] .
If you later decide to change the container type (for example to std::list ) then indexes will not work. So in general it is better to fill an array with iterators each pointing to exact matched element. And finally it is not a good idea to return a whole array, I'd prefer accepting this array by reference in my function.
Here is a modified example for your code which will fill indexes of the matched elements:
bool FindNumber(const std::vector<sStringStruct>&v, const std::string& nString, std::vector<int>& indexes)
{
bool found = false;
sStringStruct searched_string = sStringStruct(nString);
const int vec_size = v.size();
for(int i = 0; i < vec_size; i++)
{
if(v[i] == searched_string)
{
indexes.push_back(i);
found = true;
}
}
return found;
}
std::vector <sStringStruct> vec;
vec.push_back(sStringStruct("xxxx"));
vec.push_back(sStringStruct("aaaa"));
vec.push_back(sStringStruct("bbbb"));
vec.push_back(sStringStruct("cccc"));
vec.push_back(sStringStruct("aaaa"));
std::vector<int> indexes;
bool found = FindNumber(vec, "aaaa", indexes);
if(found)
{
const int indexes_size = indexes.size();
for(int i = 0; i < indexes_size; i++)
{
std::cout << "MATCH:" << vec[indexes[i]].m_nString << std::endl;
}
}
You asked about indexes, so I removed the usage of std::find algorithm.
But as I've said before, this solution works only for std::vector .
More general solution is to fill an array of iterators using std::find algorithm.
I hope this helps.
Regards
|
|
|
|
|
Yes, it helped. Thanks a lot for your answers. I get the point now.
|
|
|
|
|
You're welcome!
|
|
|
|
|
Hi,
I have a ATL Exe server sending events(samples) to my client application. For some reason if I hold down the (X) close button on the client window without releasing it. It blocks the ATL exe server from firing the events to the client until I release the (X) button. Is this something to do with the threading model?
Regards,
Anthony
|
|
|
|
|
Kind of. COM uses window messages to communicate. When you hold down the X button, the UI thread enters a modal message loop and won't process COM related windows messages. Maybe have the event handler on a different thread in the client?
Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p
CodeProject MVP for 2010 - who'd'a thunk it!
|
|
|
|
|
Hi there,
I've written a WTL SDI application, that uses splitter windows. I'd like to know how what messages to listen for to pick up when the splitter windows are moved.
I've tried listening to WM_SIZE, but only the CMainFrame seems to send WM_SIZE messages.
Here's my CSplitterImpl derived class:
template <bool t_bVertical = true>
class CViewSplitterT : public CSplitterWindowImpl<CViewSplitterT<t_bVertical>, t_bVertical>
{
public:
DECLARE_WND_CLASS_EX(_T("Splitter"), 0, -1)
CViewSplitterT(){ }
};
typedef CViewSplitterT<false> CHorSplitter;
typedef CViewSplitterT<true> CVertSplitter;
What should I be listening for in the message map of my CMainFrame?
All suggestions welcomed!
modified on Friday, January 22, 2010 3:29 AM
|
|
|
|
|
When you don't know what messages are being sent, I'd suggest using Spy++ (distributed with Visual Studio) to look at what messages are flying around.
Using that method on a WTL app with splitter that I have, I don't think the main frame gets any notification of splitter position change. The only thing I can see is the views inside the splitter getting WM_WINDOWPOSCHANGING and WM_WINDOWPOSCHANGED messages when they are resized.
Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p
MVP for 2010 - who'd'a thunk it!
|
|
|
|
|
Have a look at this article
[^]
|
|
|
|
|
|
I need a program in PHP which can extract the hidden biders from ebay.I will have to insert the item number and the program must show me all bidders of the auction and their emails.
Also the same program must extract the registered contact information for any specified bidder specially the email address
I will pay for this
If someone can help please contact me at totos_back@yahoo.com
Thanks
|
|
|
|
|
How to copy table with Data and constraints from one database to another database
Database A have 300000 columns
I need above table to copy in Database B but with constraints I am using below Query but here constraints are misssing
SELECT * INTO Table1
FROM A.dbo.Table1
Pls suggest me correct Query
|
|
|
|
|
Wrong forum.
L u n a t i c F r i n g e
|
|
|
|
|
I could not see SQL Server Option so I Posted here. Please Take it forward
|
|
|
|
|
Try the General Database forum instead - you'll have a better chance of getting help there.
L u n a t i c F r i n g e
|
|
|
|
|
ATL ActiveX control embedded in Internet Explorer not receiving WM_CREATE, WM_DESTROY, WM_SHOWWINDOW messages.
Other messages (WM_LBUTTONDOWN for example) are successfully handled.
The same control embedded in the test application works fine (all messages are received).
Thanks in advance
Oleg
|
|
|
|
|
Hi, i am using transform function to transform a vector to a map but getting the following error.
multiMapType.obj : error LNK2001: unresolved external symbol "struct std::pair<enum PC::PC_type,class PC> __cdecl std::make_pair(enum PC::PC_type const &,class PC const &)" (?make_pair@std@@YA?AU?$pair@W4PC_type@PC@@V2@@1@ABW4PC_type@PC@@ABV4@@Z)
Debug/MultiMapType.exe : fatal error LNK1120: 1 unresolved externals
The code snippet is as follows
int main( )
{
const PC::PC_type kind[] = { PC::IBM,PC::IBM, PC::Dell, PC::HP,PC::HP, PC::HP };
const int num_appliances = 3;
vector<PC> v;
for( int i = 0; i < num_appliances; ++i )
v.push_back( PC( kind[i], i, i ) );
typedef multimap<PC::PC_type,PC> PC_multimap_type;
PC_multimap_type stock;
const PC desired( PC::HP );
transform( kind, kind+num_appliances, v.begin(),inserter( stock, stock.end() ),make_pair<PC::PC_type,PC> );
PC_multimap_type::const_iterator stock_end = stock.end();
PC_multimap_type::const_iterator spot = stock.find( desired.appliance() );
if( spot != stock_end )
spot->second.print();
else
cout << "Don't have a " << desired.name() << " in stock\n";
return 0;
}
Please help.
Thanks
|
|
|
|
|
Given that I've made assumptions about context (definitions of PC? PC_type? headers you include?), it's difficult to say if I've done the same as you.
But your code compiles and links for me under VS2008. What version are you using?
Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p
|
|
|
|
|
Hi,
Thanks for replying. I am using VS98(VC6.0) .
The headers file and defination of PC is as follows
#include <algorithm>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <string>
#include <utility>
#include <vector>
using namespace std;
class PC
{
public:
enum PC_type { Dell, HP, IBM, Compaq };
PC( PC_type appliance = Dell, int model = 220,
int serial = 0 );
bool operator<( const PC& rhs ) const;
PC_type appliance() const;
int model() const;
string name() const;
void print() const;
int serial() const;
private:
PC_type appliance_;
int model_;
int serial_;
};
|
|
|
|
|
ashtwin wrote: Thanks for replying. I am using VS98(VC6.0)
My commiserations - VC6 sucks for template usage.
Anyway - try declaring the make_pair instance you need before main():
template std::pair<PC::PC_type,PC> std::make_pair<PC::PC_type,PC>(PC::PC_type,PC);
int main( )
{
Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p
|
|
|
|
|
Hi, i tried the declaration before main
template std::pair<PC::PC_type,PC> std::make_pair<PC::PC_type,PC>(PC::PC_type,PC);
int main( ){
but still getting the same error.
May be some problem in project settings.
|
|
|
|
|
No, shouldn't be project settings. And I don't have VC6 to test with. Oh well.
Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p
|
|
|
|
|
The program is getting build properly if i use a function insted of make_pair in the last parameter of the function transform and return a pair from that function.
pair<PC::PC_type, PC> MyFunction(PC::PC_type pcType, PC pc)
{
return make_pair(pcType, pc);
}
transform( kind, kind+num_appliances, v.begin(),inserter( stock, stock.end() ),MyFunction);
|
|
|
|
|
Yes...I should have mentioned that solution In this one, you're not asking the compiler to automatically instantiate a function from the reference in transform, so yes, it includes all the necessary code.
Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p
|
|
|
|
|