How to find the most frequent value of string array, and how many
-
I have a vector string array filled as follows: "s1" "v1" "s1" "f1" "g1" "s1" "s1" "o1" so the result has to be "s1 4". I have to write the string value and the num value in strArtist and iNum. How's that?
void maxCollByArtist(string& strArtist, int& iNum)
{
int max = 0;
string t_str = "";
vector temp;for(int i = 0; i < m\_vMusic.size(); i++) temp\[i\] = m\_vMusic\[i\].GetArtist(); for(int i = 0; i < temp.size(); i++) { //if(temp\[i\] == t\_str) }
}
-
I have a vector string array filled as follows: "s1" "v1" "s1" "f1" "g1" "s1" "s1" "o1" so the result has to be "s1 4". I have to write the string value and the num value in strArtist and iNum. How's that?
void maxCollByArtist(string& strArtist, int& iNum)
{
int max = 0;
string t_str = "";
vector temp;for(int i = 0; i < m\_vMusic.size(); i++) temp\[i\] = m\_vMusic\[i\].GetArtist(); for(int i = 0; i < temp.size(); i++) { //if(temp\[i\] == t\_str) }
}
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
-
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 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;
}
-
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 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 = 1for 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 -
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 = 1for 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 = 0It'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 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;
}
-
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 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.
-
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?
-
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 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;
}
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 v_strAll; vector v_strUnique; list> 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::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;})));});
-
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 v_strAll; vector v_strUnique; list> 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::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;})));});
:thumbsup: +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.
-
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.
Here is my code, but it shows "asd 2" instead of "qwe 3". Why is that?
#include
#include
#include
#includeusing namespace std;
int main()
{
string str[8] = {"zxc", "zxc", "asd", "qwe", "qwe", "asd", "qwe", "jkl"};
vector 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;
}
-
Here is my code, but it shows "asd 2" instead of "qwe 3". Why is that?
#include
#include
#include
#includeusing namespace std;
int main()
{
string str[8] = {"zxc", "zxc", "asd", "qwe", "qwe", "asd", "qwe", "jkl"};
vector 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.