简体   繁体   中英

Find number of letters in a vector of strings c++

I have a vector of strings example: dedf eedf fedf hedf

I want to go through the list and count how many times each letter appears

so for example the letter d appears 5 times e appears 5 times f appears 5 times h appears 1 time

I don't have no code so far but I'm trying to see how I can do this with logic first.

I'm trying to code but don't know where to start.

I was thinking I can store each letter into a string. string would be {dedfeedffedfhedf}

Then take the string and count each time the letter is in that string but this is where I'm having a problem. Any thoughts?

Any suggestions would also be appreciated.

Thank You

A general algorithm might be:

create empty array/map/storage container for counting
for each string in the vector
   for each character in the string
       counter[character] += 1

You could do this a few ways (pseudocode, of course):

for each letter you are interested in:
    for each character in the string:
        if letter matches character:
            increment counter
    print letter and counter

or

declare array of counters, one for each letter
for each character in the string:
    if character is a letter:
        increment that counter in the array
print counters from array

or

sort the characters in the string
for each character in the sorted string:
    if character is a letter:
        count the number of times that letter occurs
        print letter and count

Each of these methods will have different performance characteristics. Some trade off space (in a counter array) for extra time (nested loops or a sort). See if you can determine which one has the best performance for your situation.

You can have an array for holding the counts of each letter. If we're assuming just the alphabet, you'll have an array of 26 elements (likely ints), all initialized to 0. Then you can go through each string, and each time you encounter a character, you increment that count.

//let's call your vector of strings stringvec
int counts[26];

//initialize counts to 0

//go through each string in the vector
for (int i = 0; i < stringvec.size(); i++) {
    //get current string
    string curstr = stringvec[i];

    //for each letter in the string
    for (int j = 0; j < curstr.size(); j++) {
        //curstr[j] is a character at position j in the string
        //subtracting 'a' from it will give you the position in relation to 'a' in ASCII, so a character 'a' = 0, 'b' = 1, 'c' = 2, and so on...
        counts[curstr[j] - 'a']++;
    }
}

And then you do whatever you want with the counts.

It would be smart to use array to store count of letters, so that you are able to access count of randomly chosen letter in O(1).

int letters[26] = {0};
...
char c;
if (c >= 'a' && c <= 'z')
    letters[c - 'a']++;
...
return 0;

Check this lecture by Richard Buckland (video) - 15:20 starts the part that will help you ;)

#include <iostream>
#include <vector>
#include <string>
#include <unordered_map>

using namespace std;

typedef vector<string> StrVector;
typedef unordered_map<char, int> CharIntMap;

int main() {
    //the following code will work only with a c++11 compiler
    StrVector words = {"dedf", "eedf", "fedf", "hedf"};
    CharIntMap counts;
    for (string &word : words) {
        for (char &letter : word) {
            counts[letter]++;
        }
    }
    for (auto it : counts) {
        cout << "Count of letter " << it->first << " = " << it->second << endl;
    }
    return 0;
}

You need a data structure which allows you to map a letter to a count. Iterate through the vector, and iterate through each character in the string, and look in the map for the character, and increment the count.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM