简体   繁体   中英

Why are pointers used for std::string.find?

Originally, I used string_array.find(item) to search, but the compiler said no.

Then I searched to see if we can use string_array->find(item) to solve the problem by using pointer, but there is no explanation.

Here is my code:

#include<iostream>
#include<string>
using namespace std;
string name [100];
int Jan[100]={0}, Feb[100]={0}, Mar[100]={0}, Apr[100]={0}, May[100] = 0, Jun[100]={0},Jul[100]={0}, Aug[100]={0},Sep[100]={0},Oct[100]={0},Nov[100]={0},Dec[100]={0};
int earning [100];
int main () {
    ifstream load;
    load.open("record.txt");
    if(load.fail()){
        cout << "error in loading data" << endl;
        exit(1);
    } 
    else {
int i = 0;
    string NAME;
    int EARNING;
    int MONTH;
    int dump1;
    char dump2;
    while ( load.eof() != 1 ) {
        int n = 0;
        load >> NAME >> EARNING >> dump1 >> dump2 >> MONTH >> dump2 >> dump1 ;
        n = name->find(NAME);
        if ( n < 0 ) {
            n = i ;
        } 
        else {
    i = i - 1 ;
        }
        name[n] = NAME;
        if ( MONTH == 1) { Jan[n] += EARNING ;}
        if ( MONTH == 2) { Feb[n] += EARNING ;}
        if ( MONTH == 3) { Mar[n] += EARNING ;}
        if ( MONTH == 5) { Apr[n] += EARNING ;}
        if ( MONTH == 5) { May[n] += EARNING ;}
        if ( MONTH == 6) { Jun[n] += EARNING ;}
        if ( MONTH == 7) { Jul[n] += EARNING ;}
        if ( MONTH == 8) { Aug[n] += EARNING ;}
        if ( MONTH == 9) { Sep[n] += EARNING ;}
        if ( MONTH == 10) { Oct[n] += EARNING ;}
        if ( MONTH == 11) { Nov[n] += EARNING ;}
        if ( MONTH == 12) { Dec[n] += EARNING ;}
        i++; 
        }
    }
}

name is an array of 100 strings. If you use an array name by itself like you are, it decays into a pointer to its first element. Your code:

n = name->find(NAME);

Is equivalent to:

n = (&name[0])->find(NAME);

So you can see how that value is a pointer and needs to be operated on with -> rather than . . If it helps, a->b is equivalent to (*a).b .

I think your code is buggy though; are you really sure that's how you want it to work?

The fact with this line:

n = name->find(NAME);

is that it is not doing what you think. Here you are using the std::find method on a std::string . The first element of the array. Not the array itself. In addition, this line of code is no different than:

n = (*name).find(NAME);

So, your code is searching if NAME is present in the first element of your string array. To search an entire array, I would suggest you use a std::vector . It gives you access to the standard iterator methods:

#include <vector>

std::vector<std::string> name(100);
// ...
it = find(name.begin(), name.end(), NAME);
// std::find return an iterator
if (it == name.end())
    // NOT FOUND
else
    // FOUND

The fact that it compiles is a coincidence - it just calls find on the first string from the array. I suppose you wanted to find the string within the array, so you should be using something like std::find :

string *begin = name;
string *end = begin + 100;

if (end == std::find(begin, end, NAME))
{
  // not found
}

But it would be probably better to switch to std::vector<std::string> .

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