Function that counts unique letters in a string

Well I'm trying to make a function that reads a string from a vector, counts the number of unique characters in each string, and places the results in a new vector. This is essentially my first draft, could use some input with getting it to work. Any help is much appreciated!

1
2
3
4
5
6
7
8
9
10
11
errors:

c:\program files (x86)\codeblocks\mingw\lib\gcc\mingw32\4.8.1\include\c++\backward\backward_warning.h|32|warning: #warning This file includes at least one deprecated or antiquated header which may be removed without further notice at a future date. Please use a non-deprecated interface with equivalent functionality instead. For a listing of replacement headers and interfaces, consult the file backward_warning.h. To disable this warning use -Wno-deprecated. [-Wcpp]|
C:\Users\netodude\Desktop\countlettersinvec\countletters.cpp||In function 'Vector<int> counts(Vector<String>)':|
C:\Users\netodude\Desktop\countlettersinvec\countletters.cpp|8|error: invalid use of member function (did you forget the '()' ?)|
C:\Users\netodude\Desktop\countlettersinvec\countletters.cpp|12|warning: comparison between signed and unsigned integer expressions [-Wsign-compare]|
C:\Users\netodude\Desktop\countlettersinvec\countletters.cpp|14|warning: comparison between signed and unsigned integer expressions [-Wsign-compare]|
C:\Users\netodude\Desktop\countlettersinvec\countletters.cpp|21|error: invalid types '<unresolved overloaded function type>[int]' for array subscript|
c:\program files (x86)\codeblocks\mingw\lib\gcc\mingw32\4.8.1\include\c++\bits\ios_base.h|786|error: 'std::ios_base::ios_base(const std::ios_base&)' is private|
c:\program files (x86)\codeblocks\mingw\lib\gcc\mingw32\4.8.1\include\c++\bits\basic_ios.h|66|error: within this context|
c:\program files (x86)\codeblocks\mingw\lib\gcc\mingw32\4.8.1\include\c++\istream|58|note: synthesized method 'std::basic_ios<char>::basic_ios(const std::basic_ios<char>&)' first required here |


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
vector<int> counts (vector<string> tmp)
{
    vector<int> results;
    int ulcount; //unique letter count

    for (int i = 0; i < tmp.size; ++i)
    {
        ulcount = 0;
        string cpy = tmp[i];
        for (int j = 0; j < cpy.size(); j++)
        {
            for (int k = 0; k < cpy.size(); ++k){
                if (cpy[j]==cpy[k]){ //if letter in position j ever equals k, break and try next letter
                break;
                }
                ++ulcount;
            }
            
        }
        results.push_back[ulcount];
    }
    return results;
}
Last edited on
quick spot:
tmp.size
should be
tmp.size()
Ah, thank you, didn't catch that.

This is the whole code that I'm working with, after I changed a couple of things:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#include "std_lib_facilities.h"

vector<int> counts (vector<string> tmp)
{
    vector<int> results;
    int ulcount; //unique letter count

    for (int i = 0; i < tmp.size(); ++i)
    {
        ulcount = 0;
        string cpy = tmp[i];
        for (int j = 0; j < cpy.size(); j++)
        {
            for (int k = 0; k < cpy.size(); ++k)
            {
                if (cpy[j]==cpy[k+j])  //if letter in position j ever equals k, break and try next letter
                {
                    break;
                }
                else ++ulcount;
            }

        }
        results.push_back(ulcount);
    }
    return results;
}

void show_results(vector<int> res)
{
    for (int i=0; i<res.size(); ++i)
    {
        cout << res[i] << " ";
    }
}


int main()
{

    vector<string> input;
    vector<int> counted;
    vector<int> results;

    while(cin != ";")
    {
        input.push_back(cin);
    }
    counts (input);

    show_results(results);

}



But now I'm getting these errors:
1
2
3
4
5
6
7
8
9
10
11
12
13

c:\program files (x86)\codeblocks\mingw\lib\gcc\mingw32\4.8.1\include\c++\bits\ios_base.h|786|error: 'std::ios_base::ios_base(const std::ios_base&)' is private|
c:\program files (x86)\codeblocks\mingw\lib\gcc\mingw32\4.8.1\include\c++\bits\basic_ios.h|66|error: within this context|
c:\program files (x86)\codeblocks\mingw\lib\gcc\mingw32\4.8.1\include\c++\istream|58|note: synthesized method 'std::basic_ios<char>::basic_ios(const std::basic_ios<char>&)' first required here |
C:\Users\netodude\Desktop\countlettersinvec\countletters.cpp||In function 'int main()':|
C:\Users\netodude\Desktop\countlettersinvec\countletters.cpp|47|note: synthesized method 'std::basic_istream<char>::basic_istream(const std::basic_istream<char>&)' first required here |
C:\Users\netodude\Desktop\countlettersinvec\std_lib_facilities.h|106|error:   initializing argument 1 of 'String::String(S) [with S = std::basic_istream<char>]'|
C:\Users\netodude\Desktop\countlettersinvec\std_lib_facilities.h||In instantiation of 'String::String(S) [with S = std::basic_istream<char>]':|
C:\Users\netodude\Desktop\countlettersinvec\countletters.cpp|47|required from here|
C:\Users\netodude\Desktop\countlettersinvec\std_lib_facilities.h|106|error: invalid user-defined conversion from 'std::basic_istream<char>' to 'const char*' [-fpermissive]|
c:\program files (x86)\codeblocks\mingw\lib\gcc\mingw32\4.8.1\include\c++\bits\basic_ios.h|115|note: candidate is: std::basic_ios<_CharT, _Traits>::operator void*() const [with _CharT = char; _Traits = std::char_traits<char>] <near match>|
c:\program files (x86)\codeblocks\mingw\lib\gcc\mingw32\4.8.1\include\c++\bits\basic_ios.h|115|note:   no known conversion for implicit 'this' parameter from 'void*' to 'const char*'|


And I realized i put this in the wrong forum. Should've been in beginners :(
Firstly i replaced your #include with:
1
2
3
4
5
#include <iostream>
#include <vector>
#include <string>


just to make sure i'm dragging in only what I want.

Your issues are then here:
1
2
3
4
 while(cin != ";")
    {
        input.push_back(cin);
    }


Your input vector is a string container by your definition so i'm not sure what you're trying to do by passing in a 'cin'.

what should that vector actually contain after that while loop?
After that while loop, the vector should contain the strings that the user inputted.
I made some changes to that part, it now looks like this:
1
2
3
4
5
    while (cin >> in)
    {
        input.push_back(in);
        if (in==";") break;
    }


It collects input correctly and stops after the semicolon,, except that the semicolon has to be by itself to stop. The input can't be "fred;" rather, it has to be "fred ;"
The main thing I need help with fixing is the counts function. It should count the unique letters in each string and push the results into the results vector.

Example: "bob" has 2 unique letters, push 2 into results.
"bobby" has 3 unique letters, push 3 into results.
Last edited on
then i think (if i understand) you need a std::map in your counts function.

map should be either:
std::map<char, int>
or
std::map<std::string, int>

where the char or string is the letter found, and the int is the count of that letter so far.

for each string:
- iterate over the string
- check to see if this char is in your map. if it is, increment the count in the map.
- if not, add the char/string to your map and set it's count to 1.
Topic archived. No new replies allowed.