1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
|
#include <string>
#include <sstream>
#include <iostream>
int main()
{
std::string n[5][5] = {
{ "1", "2", "3", "4", "5" },
{ "6", "7", "8", "9", "10" },
{ "11", "12", "13", "14", "15" },
{ "16", "17", "18", "19", "20" },
{ "21", "22", "23", "24", "25" }
};
for (int i = 0; i < 5; i++)
{
for (int j = 0; j < 5; j++)
{
const std::string& s = n[i][j];
std::istringstream iss(s);
int value;
char c;
if (iss >> value) // conversion successful.
{
c = static_cast<char>(value);
std::cout << "ascii value " << value << " = " << c << std::endl;
}
else
{
std::cout << "failed to convert " << s << std::endl; // handle conversion error.
}
}
}
}
|