Home > Software engineering >  My program use only one method, but I have other method
My program use only one method, but I have other method

Time:10-15

I have problem with this simple code. The program counts the letters in the string / vector.

I have two methods, one for vector and one for string. In main(), on the first line, I want to use the method for vector in first parameter, and on the second line I want to use the method for string. But both commands are using the string method.

#include <iostream>
#include <vector>
using namespace std;

int How_many_vector=0;
int How_many_string=0;

int count_chars(vector<char>& word, char character){
    for(int i = 0; i <=word.size();i  ){
        if(word[i]==character){
            How_many_vector  ;
        }
    }

    return How_many_vector;
}

int count_chars(string wordstring,char character){
    for(int i = 0;i<=wordstring.length();i  ){
        if (wordstring[i]==character){
            How_many_string  ;
        }
    }
    return How_many_string;
}

int main(){
    cout<< count_chars({'t','a','b','t','z'},'t')<<endl;
    cout<<count_chars("balcer",'b');
}

CodePudding user response:

Both std::vector and std::string can be constructed from a brace list. So you have to be more explicit about which function you actually want to call when passing it a brace list.

You can't bind a vector& reference to a brace list. And you can't bind it to a temporary vector, either. So the 1st line can't call the vector version of the function.

However, a string object can be constructed from a brace list, and the string version of the function takes a string by value, not by reference. So, the 1st line can (and does) call the string version of the function, by creating a temporary string from the brace list, and then passing that temporary to the function.

If you want to call the vector version instead, you will have to call it with a pre-existing vector object for the vector& reference to bind to:

#include <iostream>
#include <vector>
#include <string>
using namespace std;

int count_chars(vector<char>& word, char character){
    int How_many = 0;
    for(size_t i = 0; i < word.size();   i){
        if (word[i] == character){
              How_many;
        }
    }
    return How_many;
}

int count_chars(string wordstring, char character){
    int How_many = 0;
    for(size_t i = 0; i < wordstring.length();   i){
        if (wordstring[i] == character){
              How_many;
        }
    }
    return How_many;
}

int main(){
    vector<char> vec{'t','a','b','t','z'};
    cout << count_chars(vec, 't') << endl;
    cout << count_chars("balcer", 'b');
}

If you don't want to declare a separate variable, then change the vector& reference to const vector& instead (which you should do anyway, since the function is just reading values, not modifying them), and then you can construct a temporary vector object directly in the parameter for it to bind to (as a const reference can bind to a temporary):

#include <iostream>
#include <vector>
#include <string>
using namespace std;

int count_chars(const vector<char>& word, char character){
    int How_many = 0;
    for(size_t i = 0; i < word.size();   i){
        if (word[i] == character){
              How_many;
        }
    }
    return How_many;
}

int count_chars(const string &wordstring, char character){
    int How_many = 0;
    for(int i = 0; i < wordstring.length();   i){
        if (wordstring[i] == character){
              How_many;
        }
    }
    return How_many;
}

int main(){
    cout << count_chars(vector<char>{'t','a','b','t','z'}, 't') << endl;
    cout << count_chars("balcer", 'b');
}

And just FYI, both of your functions are redundant, as the standard library has a std::count() algorithm for counting the number of occurrences of a given value in a range of values, such as vectors and strings.

CodePudding user response:

"simple as possible" is quite simple, you do that by using library functions. By the way, code on this site is licensed cc-by-sa, so you need to cite me when you hand this in:

#include <algorithm>
#include <iostream>
#include <string>
#include <vector>

using std::count;

int main() {
    std::vector<char> v{'t', 'a', 'b', 't', 'z'};
    std::string s("balcer");

    std::cout << count(v.begin(), v.end(), 't') << "\n"
              << count(s.begin(), s.end(), 'b') << "\n";
}

CodePudding user response:

Here's code for counting letters in a string:

unsigned int letter_quantity(const std::string& s)
{
    unsigned int quantity = 0;
    const unsigned int length = s.length();
    for (unsigned int i = 0; i < length;   i)
    {
        if (std::isalpha(s[i]))   quantity;
    }
    return quantity;
}

This is simple. Increments a count for every letter in a std::string. The code can be adapted to using std::vector<char> as well.

You can make it more complex by writing your own letter detector:

for (unsigned int i = 0; i < length;   i)
{
    static const std::string letters[] =
        "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
        "abcdefghijklmnopqrstuvwxyz";
    std::string::size_t position = letters.find(s[i]);
    if (position != std::string::npos)   quantity;
}
  •  Tags:  
  • c
  • Related