掲示板システム
ホーム
アクセス解析
カテゴリ
ログアウト
fgets後のmap処理について (ID:57276)
名前
ホームページ(ブログ、Twitterなど)のURL (省略可)
本文
もう解決しているので、私が書いたものを 参考にどうぞ。 #pragma warning (disable:4786) #include <iostream> #include <fstream> #include <string> #include <vector> #include <map> void split( const std::string& strLine, std::vector< std::string >& vecData, const std::string& strDelimiter ) { std::string::size_type nIndex = 0; std::string::size_type nNext = 0; std::string::size_type nLength = strLine.length(); std::string::size_type nDelimiterLen = strDelimiter.length(); vecData.clear(); while ( nIndex = strLine.find( strDelimiter, nNext ), nIndex != -1 ) { vecData.push_back( strLine.substr( nNext, nIndex - nNext ) ); nNext = nIndex + nDelimiterLen; } if ( nNext <= nLength ) { vecData.push_back( strLine.substr( nNext ) ); } } int main( void ) { std::string strLine, strDelimiter( "," ); std::vector< std::string > vecLineData; std::map< std::string, std::string > mapData; std::ifstream finData( "test.csv" ); if ( !finData.is_open() ) { return EXIT_FAILURE; } while ( std::getline( finData, strLine ) ) { split( strLine, vecLineData, strDelimiter ); if ( vecLineData.size() < 2 ) { continue; } mapData[ vecLineData.at( 0 ) ] = vecLineData.at( 1 ); } std::map< std::string, std::string >::const_iterator it; for( it = mapData.begin(); it != mapData.end(); it++ ) { std::cout << it->first << ":" << it->second << std::endl; } return EXIT_SUCCESS; } 環境 OS : WindowsXp SP6 Pro コンパイラ,IDE : Visula C++6.0 SP6 Stand
←解決時は質問者本人がここをチェックしてください。
更新する
戻る
掲示板システム
Copyright 2021 Takeshi Okamoto All Rights Reserved.