There is such code:

int analysis::_indexOfSymbol(char symbol, std::string* alphabet, int size) { for (int i = 0; i < size; ++i) { if(symbol == alphabet[i]) return i; } return NULL; } 

The compiler gives an error that it is impossible to compare the int and the строку . Although, symbol is a type of char (yes, it is the letter equivalent of the code, but nonetheless). I tried to convert symbol to string , but neither (std::string) symbol , nor std::string(symbol) , or std::string k = std::symbol(symbol) helped me. Likewise, I tried to convert alphabet[i] to char (ATTENTION! String values ​​are stored in the array array, but they are single-character), but it didn't work for me because of the difference between the char* and char types.

The code that creates the alphabet looks like this:

 this->alphabet[0] = "А"; this->alphabet[1] = "Б"; и т.д. 
  • sizeof(alphabet) o_O And what do you think is the meaning of this expression? - VladD
  • 3
    You would have the code that the alphabet creates led ... (and the words of the vehicle are just his words) - avp
  • Since the alphabet is indeed an array of pointers to strings, then in _indexOfSymbol along with it, you must pass its size (or terminate this array with a null pointer (like argv[] in main() parameters) - avp
  • Well, I corrected there. He is now passing. - kot_mapku3
  • The answer is actually already there, I wrote it there below, but I did not add the size of the array here, I’ll correct it now. - kot_mapku3

3 answers 3

It seems to me that like this:

 if(alphabet[i].size() == 1 && alphabet[i][0] == symbol) return i; 

will be most effective.

  • And those. because I have an array of strings, then I can first refer to the element of the array, and then to the 0th element of the string. This is great) But the condition is rather long for me) - kot_mapku3
  • one
    Make a macro replacing it (or static inline function) - avp

Your alphabet is not a string, but a pointer to a string. Well, sizeof(alphabet) gives the size of the pointer (that is, 4 or 8, depending on your platform), and not the size of the string.

In addition, the size of std::string is considered wrong. The line itself is allocated somewhere in the heap, and sizeof gives the size of only the object itself, that is, the pointers it contains.

The correct loop is:

 for (int i = 0; i < alphabet->size(); ++i) { if (symbol == (*alphabet)[i]) return i; } 

In addition, the final return NULL , apparently, is wrong, because NULL is just 0, which means that the returned value will look as if the symbol was found at the zero position.


However, you can simply not build a bike, but use the built-in search function: alphabet->find(symbol) . If the character is not found, it will return the constant std::string::npos .


If the pointer actually matches an array of strings, then you must pass its size as a separate parameter:

 int analysis::_indexOfSymbol(char symbol, std::string* alphabet, size_t array_size) { for (int i = 0; i < array_size; ++i) { // здесь можно искать вхождение символа: auto pos = alphabet[i].find(symbol); if (pos != std::string::npos) return i; } return -1; // или какое-то другое значение, не могущее быть индексом } 

You cannot get the size of the array from the pointer.

An alternative way is to use std::vector instead of an array (link to), so he knows the size of his data.

[Or you can make the method template and take std::array , but this option may be appropriate if the data size is dynamic.]

  • On return NULL; the compiler will swear a bit - avp
  • 2
    And if the TC alphabet is in fact an array of string (std :: string ab [256];)? Then what ->size() in question? - avp
  • You did not quite answer. The alphabet is an array of strings, not a string. The problem is in comparison with the element of the array, and not with the string alphabet. - kot_mapku3
  • Thanks for the find, did not know about it. - kot_mapku3
  • @ kot_mapku3: Ok, updated the answer. - VladD

Connect <string> and use std::to_string . For your case: std::to_string(symbol) .