To convert an array of strings to an array of integers in C++, you can use the `stoi` function from the `string` library to parse each string element into an integer.
In this example,
strArray
containing string representations of integers.intArray
with the same length as strArray
.strArray
into an integer using the stoi
function.intArray
to demonstrate the conversion.#include <iostream>
#include <string>
int main() {
std::string strArray[] = {"10", "20", "30"};
int intArray[sizeof(strArray) / sizeof(strArray[0])];
for (int i = 0; i < sizeof(strArray) / sizeof(strArray[0]); i++) {
intArray[i] = std::stoi(strArray[i]);
}
for (int i = 0; i < sizeof(intArray) / sizeof(intArray[0]); i++) {
std::cout << intArray[i] << " ";
}
return 0;
}
10 20 30
In this tutorial, we learned How to Convert an Array of Strings to an Array of Integers in C++ language with well detailed examples.