i have a vector, it is full of frequencies of letters i counted in a file. I now want to delete anything with a frequency of zero
What I am thinking is to go through the vector and if it's greater than 0 copy it to another vector, but dont know how.
It would also help if I could keep track of the letters and frequency they occurred. any help is appreciated
C++ help, deleteing 0's from vector?
this is one way i was working on
for (unsigned int i = 0; i%26lt;freqs.size(); i++)
{
if(freqs[i]%26gt;0)
{
newFreqs[i] == freqs[i]; ///comparison not assignment
}
}
newFreqs[i] = freqs[i]; //that's assignment
I'd say ur on the right track...
But I would have an array, 26 slots (ea alphabet)...
int newFreqs[26];
char ch;
while(!done)
{
ch = read(filename, nextchar) //some func reads file, ea char
ch = tolower(ch); // make lower case
ch = ch - 'a'; //make an integer, 0 - 25.
newFreqs[ch]++; //increment occurance...
}//read next char
You see? Each array position that corresponds to a letter in the alphabet -- where a C is treated the same as a c --has a count of the number of occurances for that letter. In short, we stored the frequencies. Omit any value (zero, whathaveyou) you like by skipping over its array position.
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment