简体   繁体   中英

How do effectively use the string data_type in C++?

I tried making this small program that takes input and checks for vowels. If there are vowels then it appends them to a string and returns the size of the string.

My only problem is I can't get it to work using strings. What is the major difference over using character arrays? I can get the program to work using something like:

char entered[128];
//and then
char exceptions[11] = "aeiouAEIOU";

**Quick question about the above array. When I assign the buffer to 'exceptions' it has to be 11 or the compiler will error. Must I manually account for the NULL termination portion?

If I do something like:

if(cPtrI[i] == 'a'){

I get an error stating unknown operator '==' ?? I thought '==' was a check operator, and '=' was an assignment operator?

no match for 'operator==' in '*((+(((unsigned int)i) * 4u)) + cPtrI) == 'a''|

AND, if I do something like: (which I thought was correct, at first)

if(*cPtrI[i] == *cPtrJ[j]){

I get the same error as above, but referencing unknown operator *:

no match for 'operator*' in '**((+(((unsigned int)i) * 4u)) + cPtrI)'|
no match for 'operator*' in '**((+(((unsigned int)j) * 4u)) + cPtrJ)'|

I thought the * operator said, in effect, 'what is at' the address of where the pointer is pointing.

So, something like the above would read:

If(What is at index I of string 'a' EQUALS What is at index J of string 'exceptions'){
then ..

Any help with this one? I learned C a bit before C++, so perhaps this is where my confusing is coming from. It was my understanding the the above code would compare addresses of characters/variables they are pointing to. * indicates 'what is at' while just placing the pointer name would indicate the value the pointer is holding(which is an address of the variable being pointed to). Using &ptrName would be the address of the pointer itself, correct? Where have I gone wrong here?

#include <iostream>
#include <string>

int vowelCheck(std::string a);

int main()
{using namespace std;

    string eString;
    cout << "Enter a string: ";
        cin >> eString;
    cout << "There were " << vowelCheck(eString) << " vowels in that string.";
    return 0;
}

int vowelCheck(std::string a)
{using namespace std;

    string exceptions = "aeiouAEIOU";
    string vowels;
    string *cPtrI = &a;
    string *cPtrJ = &exceptions;

    for(int i = 0; i < a.size(); i++){
        cout << i <<"i\n";
        for(int j = 0; j < 10; j++){
            cout << j << "j\n";
           // cout << cPtrJ[j];
            if(cPtrI[i] == cPtrJ[j]){ //if index of A equal index of J then
                cout << "Added: " << cPtrJ[j];
                vowels.append(cPtrJ[j]); // append that vowel to the string 'vowels'
                break;
            }
        }
    }
    return vowels.size();
}

Using my debug tools listed above, the program will only increment through j = 8 then stops. Also, if I even enter an initial string something like AEIOU, it will string go through j = 8. So, it is not seeing the equivalent characters.

What am I doing wrong using strings?

Forget about pointers .

string *cPtrI = &a;
string *cPtrJ = &exceptions;

// ...

if(cPtrI[i] == cPtrJ[j]){ //if index of A equal index of J then

cPtrI[i] is the same as *(cPtrI + i) , which would be indexing into an array of string .

That's why cPtrI[i] == 'a' doesn't compile. cPtrI[i] has type std::string& (remember, it's indexing into a non-existing array of std::string ), and 'a' is a char . You can't compare the two.

std::string has an indexing operator of its own. Just don't use pointless pointers and it just works .

if(a[i] == exceptions[j]){

You appear to be counting the number of vowels in a string. Instead of writing out the for loops manually and building up a string, let's use count_if to do that. The plan is to create a function object that can detect if a character is a vowel and then use count_if to count the number of vowel characters in the string:

struct VowelFinder
{
    bool operator()(char c)
    {
        std::string vowels = "aeiouAEIOU";
        return vowels.find(c) != std::string::npos;
    }
};

int vowelCheck(const std::string& a)
{
    return std::count_if(a.begin(), a.end(), VowelFinder());
}

I've answered your C-related questions in comments.

As for your usage of std::string , you're actually trying to use std::string* for some reason. Don't do that. Just use the std::string ; operator [] is overloaded for it to work as-is. At the moment you're treating cPtrI as a element of an array of strings.

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