-8

このコードの何が問題になっていますか? 文字列をフロートに変換する必要があります。m2 はm2.lengthにエラーがあります

#include <iostream>
#include <string>
#include <cstdlib>
#include <sstream>
using namespace std;

int main()
{
    float v2;
    char *m2 = "1 23 45 6";
    for (int i = 0; i < m2.length(); i++) //to convert every element in m2 into float
    {
        v2 = atof(&m2[i]);
    }
    printf("%.2f", v2);

    system("pause");
    return 0; 
}
4

4 に答える 4

3

配列内の各要素を変換して、それらを格納して操作できるようにする必要があります

では、文字列ストリームを使用して、文字列から数値をベクトルに抽出するのはどうですか?

#include <iostream>
#include <iterator>
#include <sstream>
#include <vector>

int main()
{
    std::string input = "1 23 45 6";
    std::stringstream ss(input);
    std::istream_iterator<double> end;
    std::vector<double> output(std::istream_iterator<double>(ss), end);
}
于 2015-04-25T11:49:41.300 に答える
1

コードには、すべての点を説明するにはあまりにも多くの問題があります。それが私がそれをする方法です:

float v2;
std::istringstream iss("1 23 45 6");
while(iss >> v2) {
    std::cout << v2 << std::endl;
}
于 2015-04-25T11:49:35.120 に答える
0

エラーから学習するための段階的な分析:

length()まず、文字の配列にはメンバー関数がないstring m2="...";ため、コードをコンパイルするために定義する必要があります。

残念ながら、コードは 1 つの数字のみを出力します: 最後の数字です。なんで ?あなたprintf()はループの外にいるからです。

ループ内に入れると、たくさんの数値が得られますが、予想以上に多くの数値が得られ ます

6" => 23
* 3 回目の繰り返し "23 45 6" => 23 再び!
* 4 回目の反復 "3 45 6" => 3 (これを期待していましたか?)

そのため、ある番号から次の番号にジャンプする必要があります。したがって、インクリメントする代わりに、 function を使用して次のスペースを検索できますfind_first_of()

for (int i = 0; i < m2.length(); i=m2.find_first_of(' ', i+1)) // jump to the next space
{
    v2 = atof(&m2[i]);
    printf("%.2f\n", v2);
}

ここにオンラインデモがあります。

実際の C++ の代替手段:

πάντα ῥεῖの解決策を見てください。これが C++ での方法です。

于 2015-04-25T12:10:41.423 に答える
0

string,の代わりに使用char*して、物事を少し簡単にします。

指定された文字列を数値に変換するには、stringstreamまたはを使用しますatof()

問題の迅速な解決策:

#include <iostream>
#include <string>
#include <vector>
#include <stdio.h>
#include <stdlib.h>
using namespace std;

int main()
{
    vector<float> myFloats;
    string m("1. 2.23 45 6456    72.8  46..");

    // First of parse given string and separate char groups that serve as a number
    // => only consider 0-9 and '.'
    if(m.size() == 0)
        return 1;

    string charGroup;
    for(int i=0; i<m.length(); i++)
    {
        if((isdigit(m.at(i)) || m.at(i) == '.'))
        {
            charGroup += m.at(i);
            if(i == m.length() - 1 && !charGroup.empty())
            {
                // in case given group is a numerical value
                myFloats.push_back((float) atof(charGroup.c_str()));

                // prepare for next group
                charGroup.clear();
            }
        }
        else if(!charGroup.empty())
        {
            if(m.at(i) == ' ')
            {
                // in case given group is a numerical value
                myFloats.push_back((float) atof(charGroup.c_str()));

                // prepare for next group
                charGroup.clear();
            }
            else charGroup.clear();
        }
    }

    // Print float values here
    if(!myFloats.empty())
    {
        cout << "Floats: ";
        for(int i=0; i<myFloats.size(); i++)
            cout << myFloats.at(i) << ", ";
    }

    getchar();
    return 0; 
}
于 2015-04-25T12:54:06.010 に答える