B
B
Bratis4ka2020-11-25 19:24:28
C++ / C#
Bratis4ka, 2020-11-25 19:24:28

How to compare elements of an array pointed to by a pointer?

I have a pointer to an array:
std::string* workAddress;
Let's say it contains the following elements {"27", "3","+" }

Then there is a cycle along the length of the array, in which if must find the sign '+'
for(int i = 0 ; i < workAdress->length( ); i++)
if(workAdress[i] == '+')
{...}

I get an error:
/home/left/projects/Kalkul/src/Calculator.cpp|35|error: no match for 'operator ==' (operand types are 'std::string' {aka 'std::__cxx11::basic_string'} and 'char')|
How can I compare char with *name[i]?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
K
KotomeNami, 2020-11-26
@Bratis4ka

For example something like this:

int count = 3; // здесь у тебя количество элементов в массиве
std::string * obj1 = new std::string[count]; // здесь ты выделяешь под массив память
  
for (int i = 0; i < count; i++) // заполнение массива тем, что нужно
{
  if (0 == i) obj1[i] = "27";
  else if (1 == i) obj1[i] = "3";
  else if (2 == i) obj1[i] = "+";
  else obj1[i] = "XX";
}
  
for (int i = 0; i < count; i++) // твой цикл
  if (obj1[i] == "+") { obj1[i] = "222"; }

T
Timur Pokrovsky, 2020-11-25
@Makaroshka007

1. Cannot be compared with , only with a string. 2. will simply return the length of the first string in the array. Either store the length separately or use vectors std::stringchar
workAdress->length()

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question