【问题标题】:C++ Arrays: conversion of contained typeC++ 数组:包含类型的转换
【发布时间】:2010-09-21 23:48:25
【问题描述】:

在 C++ 中将固定长度的字符串数组转换为固定长度的整数数组的最佳方法是什么?

【问题讨论】:

  • 你的意思是“固定长度的字符数组到固定长度的整数数组”吗?
  • 这是在序列化的上下文中 ({ "AString", "Another" } -> { {65,102,...}, {65,78,...} }? 解析吗?( {"0","2","32"}->{0,2,32} ) 请提供更多信息。

标签: c++ arrays


【解决方案1】:

这会将一个字符数组复制到一个整数数组中:

#include <algorithm>
char foo[9] = "asdfasdf";
int bar[9];
std::copy(foo, foo+9, bar);

std::copy

这会分配一个空字符数组的值 {'a', 's', 'd', 'f', 'a', 's', 'd', 'f', '\0' } 到一个整数数组,产生 {97, 115, 100, 102, 97, 115, 100, 102, 0}。请注意,这包括原始字符串的空终止。


这将解析一个字符串数组,并将它们的整数值放入一个整数数组中:

#include <algorithm>
#include <sstream>
#include <string>

template <class T>
T parse(const std::string& str)
{
    T temp;
    std::istringstream iss(str);
    iss >> temp;
    if(iss.bad() || iss.fail())
    {
        // handle conversion failure
    }
    return temp;
}

...

std::string foo[3];
int bar[3];
foo[0] = "67";
foo[1] = "11";
foo[2] = "42";

std::transform(foo, foo+3, bar, parse<int>);

std::transform

这会将数组 foo 中的每个字符串转换为整数,并将它们放入整数数组 bar 中。

【讨论】:

  • 我认为他的意思是 string 数组,而不是 char 数组。例如:char *foo[] = {"fred", "joe", "jim");
  • 添加了第二种方法来解决这个问题。
  • 投票,但个人会使用 atoi(char*) 而不是 std::istringstream。
【解决方案2】:
#include <algorithm>

std::string foo[9];
int bar[9];

std::transform(foo, foo+9, bar, MyMagicStringToIntFunction);

其中 MyMagicStringToIntFunction 是您希望用于将字符串转换为整数的函数。由于您没有具体说明您希望如何完成,所以我无法回答这部分。

这是我对您想要做什么的猜测,但更多信息会有所帮助。 (“字符串数组是指 std::strings 的数组吗?您希望如何执行转换?”

无论如何,std::transform 是我最好的,但你必须自己填补空白。

【讨论】:

    【解决方案3】:

    如果您想使用向量而不是数组,这可能会给您一些想法。

    #include <iostream>
    #include <string>
    #include <vector>
    #include <iterator>
    #include <ostream>
    #include <algorithm>
    #include <sstream>
    using namespace std;
    
    int int_from_string(const string& s) {
        istringstream ss(s);
        int i;
        ss >> i;
        return i;
    }
    
    vector<int> int_vec_from_string_vec(const vector<string>& vstr) {
        vector<int> v(vstr.size());
        transform(vstr.begin(), vstr.end(), v.begin(), int_from_string);
        return v;
    }
    
    int main() {
        const vector<string> vstr(3, "45");
        const vector<int> vint = int_vec_from_string_vec(vstr);
        copy(vint.begin(), vint.end(), ostream_iterator<int>(cout, "\n"));
    }
    

    【讨论】:

      【解决方案4】:

      字符串数组 -> 整数数组

      循环遍历字符串数组,并使用std::istringstream将每个字符串依次转换为对应的整数

      std::size_t const N = 3;
      std::string a[N] = { "10", "-2", "5" };
      int b[N];
      
      for(std::size_t i = 0; i < N; i++) {
          std::istringstream sstream(a[i]);
          sstream >> b[i];
      }
      

      如果您有许多值,则不断重新创建字符串流可能会引入大量开销。您可以将其排除在循环之外。

      std::stringstream sstream;
      for(std::size_t i = 0; i < N; i++) {
          sstream << a[i];
          sstream >> b[i];
          sstream.clear(); 
          sstream.seekp(0); sstream.seekg(0);
      }
      

      char 数组 -> int 数组

      如果你想将一个char数组a转换成一个int数组b,你可以这样做:

      std::size_t const N = 15;
      char a[N] = { "this is a test" };
      int b[N];
      
      for(std::size_t i = 0; i < N; i++)
          b[i] = (int)(unsigned char) a[i];
      

      转换为unsigned char 使 int 数组的值变为正数(如果您想将扩展的 8 位字符转换为有符号 8 位字符类型表示为负数,否则也将是负整数) .如果您不想要这种行为,您可以安全地省略该演员表。

      【讨论】:

        【解决方案5】:

        如果你想把一个字节数组分解成一个整数数组,你可以逐个字符地做:

        char foo[10];
        int bar[10];
        
        for(int i = 0; i < 10; ++i) {
          bar[i] = (int)foo[i];
        }
        

        但我猜这不是你要找的……

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2021-07-18
          • 1970-01-01
          • 2016-10-29
          • 2021-12-22
          • 1970-01-01
          • 2016-10-06
          • 2016-07-13
          • 1970-01-01
          相关资源
          最近更新 更多