|
else if(currep > maxrep)
{
maxrep = currep;
maxstring = t_str;
}
t_str = v_str[i];
currep = 0;
should be:
else
{
if(currep > maxrep)
{
maxrep = currep;
maxstring = t_str;
}
t_str = v_str[i];
currep = 0;
}
also, you will have to add a check after the loop, to see if the last repetition has not ended.
|
|
|
|
|
" to see if the last repetition has not ended."
what does that mean?
|
|
|
|
|
Here is my code, but it shows "asd 2" instead of "qwe 3". Why is that?
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
int main()
{
string str[8] = {"zxc", "zxc", "asd", "qwe", "qwe", "asd", "qwe", "jkl"};
vector<string> v_str;
string t_str, maxstring;
int maxrep = 1;
int currep = 1;
for(int i = 0; i < 8; i++)
v_str.push_back(str[i]);
sort(v_str.begin(), v_str.end());
t_str = v_str[0];
maxstring = t_str;
for(int i = 1; i < v_str.size(); i++)
{
if(t_str == v_str[i])
currep++;
else
{
if(currep > maxrep)
{
maxrep = currep;
maxstring = t_str;
}
t_str = v_str[i];
currep = 0;
}
}
for(int i = 0; i < v_str.size(); i++)
cout << v_str[i] << endl;
cout << endl;
cout << maxstring << " " << maxrep << endl;
return 0;
}
|
|
|
|
|
TCPMem wrote: currep = 0;
i suspect it's because you're setting currep = 1, the first time, but 0 all the other times. the first set of matches will have a higher rep count than the others.
|
|
|
|
|
TCPMem wrote: I did it. Now how to see which element repeats the most and how many times?
TCPMem,
Rather than re-inventing those algorithms... just use the ones supplied by the c++ language.
string str[8] = {"zxc", "zxc", "asd", "qwe", "zxc", "asd", "qwe", "jkl"};
vector<string> v_strAll;
vector<string> v_strUnique;
list<pair<string,size_t>> counts;
v_strAll.assign(str,str+_countof(str));
sort(v_strAll.begin(),v_strAll.end());
unique_copy(v_strAll.begin(),v_strAll.end(),back_inserter(v_strUnique));
std::vector<string>::iterator it = v_strUnique.begin();
while(it != v_strUnique.end())
{
counts.push_back(make_pair(*it,count_if(v_strAll.begin(),v_strAll.end(),[it](const std::string& s1){return s1 == *it;})));
++it;
}
Heh, if you really want to annoy the guy reviewing your code... you can get rid of that while loop and use a dual lambda if your compiler supports it.
for_each(v_strUnique.begin(),v_strUnique.end(),[&counts,v_strAll](const std::string& str){counts.push_back(make_pair(str,count_if(v_strAll.begin(),v_strAll.end(),[str](const std::string& s1){return s1 == str;})));});
|
|
|
|
|
+5 for the last bit. And here I was thinking that APL and Perl were the only write-only languages (not counting obscurities like Brainf*ck).
Cheers,
Peter
Software rusts. Simon Stephenson, ca 1994.
|
|
|
|
|
please tell me how can i read a data file into an array line by line
for e.g i have
bob 123
mike 456
|
|
|
|
|
use CStdioFile and CStdioFile::ReadString to read one file, and a nice while loop to read each line; put each line that you read into a std::vector<CString > .
If you need to split each line into section (name and number), then you will need to use something like strtok .
goood luck.
Watched code never compiles.
|
|
|
|
|
Using just stl you can do something like:
ifstream file("file.dat");
vector<string> lines;
string line;
while (getline(file, line))
lines.push_back(line);
|
|
|
|
|
i have created a server(tcp socket)...now i want to load a text file of usernames and password into the server...help please!
i am doing socket programming for first time
|
|
|
|
|
Groan.
So the problem you have is reading documentation. Read it again, and again, and again, and write some code, then read it again. Then come back to us when you have a real question.
==============================
Nothing to say.
|
|
|
|
|
What's the question?
For new socket programmers, usually the confusion seems to be in the fact that as far as the socket is concerned, you're passing data bytes, it doesn't care what the data is (integers, text, whatever). So you just have to load some buffers with your data and pass it on, it's up to your server/client to be able to interpret the data correctly. The interface definition (what bytes mean what) is usually referred to as an API (i.e. the rules).
|
|
|
|
|
Dear Developers,
I am trying to show/hide a explorer control via ShowWindow() function. Along with I have done disable/enable of that control. But application is getting crashed on it. I have written the code is like this.
GetDlgItem(IDC_EXPLORER_LICENSE)->ShowWindow ( SW_SHOW ); GetDlgItem(IDC_EXPLORER_LICENSE)->EnableWindow( TRUE );
One thing should be noted that I have done the same thing for some other controls( like static and picture ), but for that it's working properly.
Can any one help me out.
Thanks in Advance.
Amrit Agarawal
Software Developer
|
|
|
|
|
what does GetDlgItem(IDC_EXPLORER_LICENSE) return ?
|
|
|
|
|
According to the docs on GetDlgItem()
Quote: A pointer to the given control or child window. If no control with the integer ID given by the nID parameter exists, the value is NULL.
In your code, GetDlgItem() is in the context of a CWnd / CDialog. Are you sure the control IDC_EXPLORER_LICENSE is defined to be in that CDialog? The debugger should show you the value of "this" when you crash inside ShowWindow().
|
|
|
|
|
Is there anyone that can explain why a modeless dialog is blocking the parent window. I thought that the moduless dialog was an standalone thread that did not inhibit the parent process to execute.
What is differences between a background thread and a modeless dialog?
|
|
|
|
|
See here[^] for information on modeless dialog boxes.
A background thread operates totally independently of the main thread of your application. A modeless dialog box interacts with (and is partly controlled by) its parent window.
Unrequited desire is character building. OriginalGriff
I'm sitting here giving you a standing ovation - Len Goodman
|
|
|
|
|
The two concepts aren't really related. A "modeless dialog" is simply the behavior of a window, it doesn't imply what thread it's operating on, in another words, a modeless dialog (by default) will still be in the same thread as the parent window (although it doesn't have to be). Now a background thread, well, that implies complete processing independence from the main application.
|
|
|
|
|
I have an MDI application which is using CMFCVisualManager and showing nice themes
It also has some dialogs for example About dialog which is not using this theme. Can I apply this them on that? How?
I have VS 2008 + MFC featured pack with some service pack.
|
|
|
|
|
I am trying to be able to send socket commands from a MFC program but my parent window is blocked see the message below.
Is it possible to use fork to start another MFC program?
What is the simplest way to communicate with this programs?
I was thinking to using three files. One that indicates that that data is sent by Master program and another file that is used by Sölave that tells that it is processing data and a third file that data actually is stored in.
Is there a better way to do this I have just a few days to fix this problem on so it should be a simple solution?
|
|
|
|
|
See this message[^] below by enhzflep on the use of threads; it could well be what you are looking for.
Unrequited desire is character building. OriginalGriff
I'm sitting here giving you a standing ovation - Len Goodman
|
|
|
|
|
Are you sure that this will inhibit the parent window message queue/parent dialog from be blocket? What is the differences beteen create a modeless dialog comared with this thread?
Is there anyone that can explain why a modeless dialog is blocking the parent window. I thought that the moduless dialog was an own thread that did not inhibit the parent process to execute.
modified 19-Nov-11 5:24am.
|
|
|
|
|
Yes, the background thread will run independently of the main window thread so your message pump will continue to run. I am not sure whether a modeless dialog would be better, it all depends on what your program is trying to do.
Unrequited desire is character building. OriginalGriff
I'm sitting here giving you a standing ovation - Len Goodman
|
|
|
|
|
The only thing I do is sending out a message by using CSocket/CTimeoutSocket, but if fail to connect to the unit I try to access both the modeless dialog and the parent dialog is blocked.
Is there anyone that can explain why a modeless dialog is blocking the parent window. I thought that the moduless dialog was an standalone thread that did not inhibit the parent process to execute.
What is differences between a background thread and a modeless dialog?
|
|
|
|
|
I am trying to create a program in MFC that talkes to several wireless IP-routers on internet. Now and then will a router be disconnected, and when that happen will the parent window in my MFC program be blocked for a while, which is not acceptable.
The reason for this is that in my MFC program it should be possible for a user to send and receive commands to one of the router while the other routers is supervised (I sending a special command to these router to see if they is still up and running).
The problem is that if one of the routers is disconnected and my MFC program sends out a supervise command to this router will the main window be blocked which means that a user cannot use the parent window for 30 seconds or so.
I have tried to solve this with a worker thread but I could not invoke any CTimeoutSocket from this thread since the MFC program stopped working then.
So I tried to create a hidden modeless dialog and put all the CTimeoutSocket code in this dialog but when a socket is blocked for a while will not only the modeless dialog be blocket but also the parent window.
So have anyone any idea how I shall do to inhibit the parent window from be blocked?
modified 19-Nov-11 3:15am.
|
|
|
|