changing list of character to list int in C++

  c++

i try to change from string to list of char, than list of char change to list int. here is my code

int main() {
    std::string int1 = "1122334455";
    std::list<char> listChars(int1.begin(), int1.end());
    std::list<int> intList(listChars.begin(), listChars.end());
    for(const auto& elem: intList){
        std::cout << elem << " ";
    }
}

The result of this is

49 49 50 50 51 51 52 52 53 53  

not

1 1 2 2 3 3 4 4 5 5 

the second method i try to use is

void printListInt(std::list<int>& listInt){
    for(int i:listInt){
        std::cout << i;
    }

    std::cout << std::endl;
}

int main() {
// make string "Hello World" and string "1122334455"
std::string str1 = "Hello World";
std::string int1 = "1122334455";

std::list<char> listChars(str1.begin(), str1.end());
std::list<char> listCharInt(int1.begin(), int1.end());

std::list<int> listInt(listCharInt.begin(), listCharInt.end());
printListInt(listInt);
}

I understand that it has to -48 to show the expected result, but is there possibility that i don’t use for to recalculate list int?

Source: Windows Questions C++

LEAVE A COMMENT