【问题标题】:changing list of character to list int in C++在 C++ 中将字符列表更改为列表 int
【发布时间】:2021-12-02 15:02:12
【问题描述】:

我尝试从字符串更改为 char 列表,而不是 char 列表更改为 list int。这是我的代码

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 << " ";
    }
}

这样的结果是

49 49 50 50 51 51 52 52 53 53  

不是

1 1 2 2 3 3 4 4 5 5 

我尝试使用的第二种方法是

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);
}

我知道它必须 -48 才能显示预期结果,但有没有可能我不使用 for 来重新计算列表 int?

【问题讨论】:

  • 如果要将 int 打印为 char,则需要将其强制转换为 1 或进行数学运算以将 ascii 值转换为实际数值。

标签: c++ list algorithm


【解决方案1】:

您可以使用例如标准算法std::transform

这是一个演示程序。

#include <iostream>
#include <string>
#include <list>
#include <iterator>
#include <algorithm>

int main() 
{
    std::string int1 = "1122334455";

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

    std::list<int> intList;

    std::transform( std::begin( listChars ), std::end( listChars ), 
                    std::back_inserter( intList ),
                    [ ]( const auto &c ) { return c - '0'; } );
 
    for(const auto& elem: intList)
    {
        std::cout << elem << " ";
    }

    return 0;
}

程序输出是

1 1 2 2 3 3 4 4 5 5

【讨论】:

  • 谢谢,让我看看文档 std::transform..
【解决方案2】:

是的,输出是正确的,因为您声明了一个字符串,即std::string int1 = "1122334455";,然后您尝试将其打印为integer,然后编译器将根据 ASCII 自动 typecast字符串所有字符的值。

“0”的ASCII值为48

“1”的ASCII值为49

“2”的ASCII值为50

“3”的ASCII值为51

。 . “9”的ASCII值是57

所以你得到的第一个输出是

49 49 50 50 51 51 52 52 53 53 因为它是在 typecasting 从 char/string 到 int 之后打印出来的。

如果你想打印 1 1 2 2 3 3 4 4 5 5 那么有不同的方法来实现这一点,这里是其中之一

#include <bits/stdc++.h>
using namespace std;

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

    return 0;
}

或者,如果您将整个字符串打印或存储到 Integer List 中,则只需将 ascii 值减去 0(即 48)

#include <bits/stdc++.h>

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-48 << " ";
    }
}

【讨论】:

    【解决方案3】:

    for(const auto&amp; elem: intList) 更改为 for(const char&amp; elem: intList),它会正常工作。

    因为 auto 被 int 替换并且您正在打印字符 ASCII 值。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-02-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-10-24
      • 2015-10-28
      • 1970-01-01
      相关资源
      最近更新 更多