|
if you sort the vector, identical items will be next to each other so it will be simpler to count which of them repeats the most
|
|
|
|
|
I did it. Now how to see which element repeats the most and how many times? (Created a new project)
int main()
{
string str[8] = {"zxc", "zxc", "asd", "qwe", "zxc", "asd", "qwe", "jkl"};
vector<string> v_str;
string t_str;
int iNum = 0;
for(int i = 0; i < 8; i++)
v_str.push_back(str[i]);
for(int i = 1; i < v_str.size(); i++)
for(int j = v_str.size() - 1; j >= i; j--)
if(v_str[j - 1] > v_str[j])
{
t_str = v_str[j - 1];
v_str[j - 1] = v_str[j];
v_str[j] = t_str;
}
for(int i = 0; i < v_str.size(); i++)
cout << v_str[i] << endl;
return 0;
}
|
|
|
|
|
or, just use:
std::sort(v_str.begin(), v_str.end())
so if you have a sorted list of strings:
aaa, aaa, bb, cc, ccc, dd, dd, dd, dd, e, f, g
iterate through the list, check to see when the current item is equal to the last item. if it's the same, keep track of how many times it repeats. if it's different, see if the number of repetitions was greater than the max number of reps you've seen so far.
cur = vec[0]
maxrep = 1
maxstring = cur
currep = 1
for each item in vec, starting at vec[1]
if item==cur
currep++ // we have a repeat, keep track of how many repetitions
else
// the strings are different,
if currep > maxrep // was this repetition count higher than the max
maxrep=currep // it is the new max
maxstring=cur
cur=item // next item is current
currep = 0
|
|
|
|
|
It's not working. Gives me "zxc" and 1, but the correct is "qwe" and 3.
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]);
for(int i = 1; i < v_str.size(); i++)
for(int j = v_str.size() - 1; j >= i; j--)
if(v_str[j - 1] > v_str[j])
{
t_str = v_str[j - 1];
v_str[j - 1] = v_str[j];
v_str[j] = t_str;
}
t_str = v_str[0];
maxstring = t_str;
for(int i = 1; i < v_str.size(); i++)
{
if(v_str[i] == t_str)
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 << t_str << endl;
cout << maxrep << endl;
return 0;
}
|
|
|
|
|
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
|
|
|
|