|
DavidCrow wrote: Before each call to push_back(), check first to see if the word already exists.
I am unable to find ,how to find a particular word already exists in the vector? This is my problem.
Appu..
"Never explain yourself to anyone.
Because the person who likes you does n't need it.
And the person who dislikes you won't believe it."
|
|
|
|
|
Use the find() algorithm. Something like:
struct s
{
public:
s(){}
string strWord;
int nCount;
bool operator==(const s& b)
{
return strWord == b.strWord;
}
};
s s4;
s4.strWord = "word to search for";
vector<s>::iterator it = find(vec.begin(), vec.end(), s4);
if (it != vec.end())
{
}
"Love people and use things, not love things and use people." - Unknown
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne
|
|
|
|
|
are you offering fries with that ?
This signature was proudly tested on animals.
|
|
|
|
|
Thanks David.
Appu..
"Never explain yourself to anyone.
Because the person who likes you does n't need it.
And the person who dislikes you won't believe it."
|
|
|
|
|
Try something like this:
#include "stdafx.h"
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <functional>
using namespace std;
void main()
{
typedef vector<string> words_t;
words_t words;
words.push_back("1");
words.push_back("1");
words.push_back("2");
words.push_back("3");
words.push_back("4");
words.push_back("4");
words_t::iterator i = words.begin();
words_t::iterator e = words.end();
sort(i, e);
for (i=adjacent_find(i, e); i!=e; i=adjacent_find(i, e))
{
cout << *i << endl;
i = find_if(i+1, e, bind2nd(not_equal_to<string>(), *i));
}
}
Steve
|
|
|
|
|
Great ... Thanks Steve..It worked for me.
The problem is i have never worked on STL collections before.
Appu..
"Never explain yourself to anyone.
Because the person who likes you does n't need it.
And the person who dislikes you won't believe it."
|
|
|
|
|
NiceNaiduhe problem is i have never worked on STL collections before.
You should give the STL a good look. You can't master C++ without being familiar with it.
Steve
|
|
|
|
|
That's true. I will go through a good book of STL.
Is there any specific book available ?
Thanks in Advance.
Appu..
"Never explain yourself to anyone.
Because the person who likes you does n't need it.
And the person who dislikes you won't believe it."
|
|
|
|
|
This[^] book is a good one.
Steve
|
|
|
|
|
Thanks Steve.
Appu..
"Never explain yourself to anyone.
Because the person who likes you does n't need it.
And the person who dislikes you won't believe it."
|
|
|
|
|
hii
i have created one windows forms application in vc++.net 2005 .
where i want to use the vector
i included
#include <vector>
also using namespace std;
declare the vector as
std::vector<int>vFile;
int m_Number= 5 ;
vFile.pushback(m_Number);
i am getting the linker error like
Error 1 error LNK2028: unresolved token (0A000380) "extern "C" int __cdecl _CrtDbgReportW(int,wchar_t const *,int,wchar_t const *,wchar_t const *,...)" (?_CrtDbgReportW@@$$J0YAHHPB_WH00ZZ) referenced in function "public: class std::_Vector_const_iterator<struct FileNames,class std::allocator<struct FileNames> > & __thiscall std::_Vector_const_iterator<struct FileNames,class std::allocator<struct FileNames> >::operator+=(int)" (??Y?$_Vector_const_iterator@UFileNames@@V?$allocator@UFileNames@@@std@@@std@@$$FQAEAAV01@H@Z) 9julyMATDOT.obj
Please help me for resolving this....
Thanks in advance ....
Anamika
|
|
|
|
|
That's a little bit odd. The function it can't find CrtDbgReportW is part of the C Runtime Library (various MSVCRT.dll and related bits depending on version).
Check that your project is set to use it, Properties, Configuration Properties, C/C++, Code Generation, Runtime Library. Try the Multi threaded Debug /MTd option or one of the others if that gives you more/different errors.
"The secret of happiness is freedom, and the secret of freedom, courage."
Thucydides (B.C. 460-400)
|
|
|
|
|
thanks for your reply....
i tried changing the runtime libary to Multi threaded Debug /MTd but of no use .....
|
|
|
|
|
In that case it looks like it might me an include path issue. I still think it odd that this STL code which is distributed as source seems to be requiring the definition of a C Runtime function that isn't part of the standard interface in order to compile. Something subtle/nasty is going on here. Are you trying to mix code from VC6 with VS2002 or later? Do you have the C Runtime source installed with your compiler (it's optional)? I'd start by checking where your build is pulling its STL and CRT header files from. Perhaps one is coming from a much later SDK and the other from a compiler installation
"The secret of happiness is freedom, and the secret of freedom, courage."
Thucydides (B.C. 460-400)
|
|
|
|
|
I'm working on a linked list class for my latest project and i would like to be able to access any of it's data members just like an array. Is it possible to override the [] operator so that I can do this? If it's not possible, what would be the next easiest way to access my linked list's data members?
|
|
|
|
|
Yes it is possible to override the [] operator. I haven't got an example in front of me at the moment but it is very roughly:
<br />
CMyElement* CMyArray::operator[]( int iIndex )<br />
{<br />
}<br />
"The secret of happiness is freedom, and the secret of freedom, courage."
Thucydides (B.C. 460-400)
|
|
|
|
|
Thanks, I'll work on this :]
I'll post up some code later on when I've got it working.
|
|
|
|
|
On a side note: why don't you use some existing class for that ? std::list or std::vector ?
|
|
|
|
|
I didn't know they existed! :P
If I can't work this out then I'll take a look at them.
|
|
|
|
|
Sauce! wrote: If I can't work this out then I'll take a look at them.
You should definitively take a look at them (maybe not for this project but at least for the next one). I agree, it looks quite difficult at a first sight but the STL is very usefull. There are some good container classes (list, vector, map, ...) which have been tested by a lot of people. So, you don't need to reinvent the wheel and come out with something that need testing, debugging and is not flexible enough to be reused.
|
|
|
|
|
I didn't take a very in-depth look at them but from what I saw I didn't think It would be as easy to do with them what I wanted but I'll take another peek at it tonight and see what I can get out of it.
|
|
|
|
|
Hi,
I need to add many buttons to groupbox and it should get the scrollbar if the buttons dont fix in to the groupbox. In .NET we can do it through Panel control. But please tell me how to do it in MFC.
|
|
|
|
|
Anybody plz help to know the method of reding very fast from mshflexgrid in MFC.The grid contains very huge string .the total rows in that is maximum of 25000.Anybody plz supply a solution for this .
vijith vijayan
|
|
|
|
|
You better make a data model object which is only displayed in the grid.
Greetings from Germany
|
|
|
|
|
Thanks for the reply .Could u plz elobrate your solution.i didnt get it .If possible provide me a sample code .
vijith vijayan
|
|
|
|