掲示板システム
ホーム
アクセス解析
カテゴリ
ログアウト
string型のvectorに格納されたデータを数値型に変換するには? (ID:63340)
名前
ホームページ(ブログ、Twitterなど)のURL (省略可)
本文
#include <iostream> #include <string> #include <sstream> #include <vector> int main() { std::string str = "0.0013,1.05,0.0002,0.01,0.95,0.95,0.012,0.0012,0.98,0.58824,0.3887,"; // CSV -> vector<string> std::vector<std::string> data; std::string::size_type s = 0; for( std::string::size_type e = str.find_first_of( ',' ); e != std::string::npos; e = str.find_first_of( ',', s ) ) { data.push_back( str.substr( s, e - s ) ); s = e + 1; } vector<string> -> vector<double> std::vector<double> numeric; for ( int i = 0; i < data.size(); ++i ) { std::istringstream stream(data[i]); double value; stream >> value; numeric.push_back(value); } // print 'em out. for ( int i = 0; i < numeric.size(); ++i ) { std::cout << numeric[i] << std::endl; } return 0; } > という風に二次元配列にすることなのですが、可能でしょうか? 可能です。学習してください。
←解決時は質問者本人がここをチェックしてください。
更新する
戻る
掲示板システム
Copyright 2021 Takeshi Okamoto All Rights Reserved.