简体   繁体   English

将字符串另存为浮点数数组C ++

[英]Save string as array of floats C++

I am receiving from terminal the following string: 我从终端收到以下字符串:
"4 4 0.2 0.5 0.3 0.0 0.1 0.4 0.4 0.1 0.2 0.0 0.4 0.4 0.2 0.3 0.0 0.5" “ 4 4 0.2 0.5 0.3 0.0 0.1 0.4 0.4 0.1 0.2 0.0 0.4 0.4 0.2 0.3 0.0 0.5”
My objective is to save this string as an array of floats like arr=[4,4,0.2,...]. 我的目标是将此字符串保存为arr = [4,4,0.2,...]之类的浮点数组。 I do not know the size of the array in advance so depends on what the user writes. 我不知道数组的大小,因此取决于用户写的内容。 The values are always separated by a space. 值始终用空格分隔。

I have tried using std::stof(as in https://www.geeksforgeeks.org/stdstof-in-cpp/ ) , stringstream (as stated in https://www.geeksforgeeks.org/converting-strings-numbers-cc/ ) but none of them works. 我试过使用std :: stof(如https://www.geeksforgeeks.org/stdstof-in-cpp/ ),stringstream(如https://www.geeksforgeeks.org/converting-strings-numbers- cc / ),但它们都不起作用。

Trials: 试用:

cout << "Introduce the transition matrix \n";
getline (cin, trans_matrix);
std::vector<float> arr(trans_matrix.size(), 0);
int j = 0, i;
// Traverse the string
for (i = 0; trans_matrix[i] != '\0'; i++) {
    // if str[i] is ' ' then split
    if (trans_matrix[i] == ' ') {
        j++;
    }
    else {
        arr[j] = std::stof(trans_matrix[i]) // string to float
    }
}

But the compiler says: 但是编译器说:

No matching function for call to 'stof' 没有匹配函数来调用“ stof”

Your code is quite mixed up. 您的代码很混乱。 Half your code treats a string as a sequence of characters (which is correct) but the other half treats it as a sequence of floats which is not really true. 您的代码一半将字符串视为字符序列(正确),而另一半则将其视为浮点序列,但这并不是真的。 For instance 例如

std::vector<float> arr(trans_matrix.size(), 0);

this creates a vector the same size as the string. 这将创建一个与字符串大小相同的向量。 But the string size is the number of characters which is not the same as the number of floats in the string. 但是字符串大小是字符数,它与字符串中的浮点数不同。 Also

arr[j] = std::stof(trans_matrix[i]);

trans_matrix[i] is a character, it's not a string, so you can't use a function on it which converts a string to a float. trans_matrix[i]是一个字符,它不是字符串,因此您不能在其上使用将字符串转换为浮点数的函数。

I'm trying to make it clear that you can't program by writing code that's approximately right. 我试图弄清楚您不能通过编写近似正确的代码来进行编程。 You have to think carefully about what you are doing and write code that is exactly right. 您必须仔细考虑您在做什么,并编写正确的代码。 You have to be completely clear and precise about the concepts. 您必须对概念完全清楚而精确。

How would you do this if you were reading from std::cout ? 如果您正在从std::cout阅读,该怎么做? Well it's exactly the same way if you are reading from a string except you use a std::istringstream instead of std::cout . 嗯,如果您从字符串中读取内容,则使用完全相同的方法,只是使用std::istringstream而不是std::cout Here's one straightforward way. 这是一种简单的方法。

#include <sstream>

std::vector<float> arr;
std::istringstream input(trans_matrix);
float f;
while (input >> f)
    arr.pusk_back(f);

Simple, create a string stream, read the floats one at a time, add them to the vector. 简单,创建一个字符串流,一次读取一个浮点数,然后将它们添加到向量中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM