C ++에서 문자열에서 이중 변환으로 Stringstream을 사용한 예상치 못한 결과
문제
나는 현재 문자열 ( "0.1")을 가져 와서 GCC4.2를 사용하여 Xcode에서 Xcode에서 C ++를 사용하여 이중으로 변환하려고합니다.
나는 내가 꼬집은 함수를 사용하고있다 다른 질문, 그러나 함수를 사용하려고 할 때 GDB에 따른 입력은 (String) "0.1"이지만 출력은 (Double) 2.1220023981051542E-314입니다.
다음은 코드를 곧바로 복사 한 내 스 니펫입니다.
double strToDouble(std::string const &numberString)
{
istringstream inStream(numberString);
double doubleValue;
inStream >> doubleValue;
if(!(inStream && (inStream >> std::ws).eof()))
{
return 0.0;
}
return doubleValue;
};
나는 OBJ-C 대신 C ++를 사용하고 있습니다. 아마도 *nix 또는 Windows 시스템에서 컴파일되어야하기 때문입니다.
저는 자연스럽게 PHP 프로그래머이지만 속도를 높이려면 약간의 크런치가 있습니다. 그래서 편집 된 언어로하고 있습니다. 대학이 낮은 수준의 언어를 다루는 이후 오랜 시간이 지났습니다 = p. 따라서 누군가 아이디어가 있다면 대단히 감사하겠습니다 ...
Drew J. Sonne.
전체 코드 :
#include <iostream>
#include <vector>
#include <string>
#include <sstream>
using namespace std;
/*
* @var delimiters the string to define a break by
* @var str the string to break
* @var tokens where to store the broken string parts.
*/
void explode(const string& delimiters, const string& str, vector<string>& tokens)
{
// Skip delimiters at beginning.
string::size_type lastPos = str.find_first_not_of(delimiters, 0);
// Find first "non-delimiter".
string::size_type pos = str.find_first_of(delimiters, lastPos);
while (string::npos != pos || string::npos != lastPos)
{
// Found a token, add it to the vector.
tokens.push_back(str.substr(lastPos, pos - lastPos));
// Skip delimiters. Note the "not_of"
lastPos = str.find_first_not_of(delimiters, pos);
// Find next "non-delimiter"
pos = str.find_first_of(delimiters, lastPos);
}
};
/*
* @var numberString double to be converted as a string.
*/
double strToDouble(std::string const &numberString)
{
istringstream inStream(numberString);
double doubleValue;
inStream >> doubleValue;
if(!(inStream && (inStream >> std::ws).eof()))
{
return 0.0;
}
return doubleValue;
};
class Cluster {
private:
vector<double> latStore;
vector<double> lngStore;
public:
Cluster(int,string,string);
};
/*
* @var latString a comma seperated list of doubles for the latitudes.
* @var lngString a comma separated list of doubles for the longitudes.
*/
Cluster::Cluster(int newLocationLength, string latString, string lngString)
{
// mark our vectors
vector<string> latStoreString;
vector<string> lngStoreString;
// Explode our input strings.
explode(",", latString, latStoreString);
explode(",", lngString, lngStoreString);
for( int i = 0; i < latStoreString.size(); i++)
{
// Grab the latitude and store it.
string tempLat = latStoreString[i];
double latDouble = strToDouble(tempLat);
latStore.push_back( strToDouble(tempLat) );
// Grab the longitude and store it.
string tempLng = lngStoreString[i];
lngStore.push_back( strToDouble(tempLng) );
}
}
int main (int argc, char * const argv[]) {
Cluster *rect = new Cluster("0.1,0.4","0.1,0.4");
return 0;
}
해결책
이것은 STL 디버그 모드로 인해 발생할 수 있습니다. 대상의 사전 프로세서 매크로 빌드 설정에서 _glibcxx_debug 매크로를 제거하십시오.
다른 팁
대신 atof ()를 사용하지 않는 이유는 무엇입니까? 링크
코드 조각에 아무런 문제가 없습니다. 사용하는 코드를 보여줄 수 있습니까? 산출 결과
Myles가 제안한 것처럼 atof ()가 더 간단 할 수 있습니다. c_str () 멤버 함수를 호출하여 문자열 객체를 AC 문자열로 변환 할 수 있습니다.
double result = atof(inputString.c_str()); // 0.0 if a double couldn't be read
추가 정보는 다음과 같습니다. http://www.cplusplus.com/reference/clibrary/cstdlib/atof/
#include <iostream>
#include <sstream>
double strToDouble(std::string const &numberString)
{
std::istringstream inStream(numberString);
double doubleValue;
std::cout << doubleValue << '\n';
inStream >> doubleValue;
if(!(inStream && (inStream >> std::ws).eof()))
{
return 0.0;
}
return doubleValue;
};
int main()
{
std::cout << strToDouble("0.1") << '\n';
return 0;
}
위의 코드는 다음과 같은 출력을 제공합니다.
-2.36907E-39
0.1
우연히 돌아 오기 전에 DoubleValue의 값을 확인하고 있습니까?
Cluster::Cluster(string latString, string lngString)
{
// mark our vectors
vector<string> latStoreString;
vector<string> lngStoreString;
// Explode our input strings.
explode(",", latString, latStoreString);
explode(",", lngString, lngStoreString);
for( int i = 0; i < latStoreString.size(); i++)
{
// Grab the latitude and store it.
string tempLat = latStoreString[i];
std::cout << "tempLat=" << tempLat << '\n';
double latDouble = strToDouble(tempLat);
std::cout << "latDouble=" << latDouble << '\n';
latStore.push_back( strToDouble(tempLat) );
// Grab the longitude and store it.
string tempLng = lngStoreString[i];
lngStore.push_back( strToDouble(tempLng) );
}
}
int main (int argc, char * const argv[]) {
Cluster *rect = new Cluster("0.1,0.4","0.1,0.4");
return 0;
}
산출:
templat = 0.1
latdouble = 0.1
templat = 0.4
latdouble = 0.4
이러한 수정으로 코드를 실행할 수 있습니까 (CTOR에서 int 매개 변수를 사용하지 않았으므로 방금 제거했습니다). 또한 오류없이 Valgrind를 사용하여 코드를 실행했습니다 (메모리 누출 만).
당신이 당신의 구현 문제에 대한 답을 얻지 못한 것 같습니다. 다른 사람들은 atof ()를 사용하도록 제안했습니다. 구현에서 문제를 해결하려면 아래 코드를 살펴보십시오. 방법이 폭발하는 벡터 대신 중복 코드를 피하기 위해 벡터를 넣으십시오. Strtodouble에서 코드도 변경했습니다.
void explode(const string& delimiters, const string& str, vector<double>& tokens)
{
// Skip delimiters at beginning.
string::size_type lastPos = str.find_first_not_of(delimiters, 0);
// Find first "non-delimiter".
string::size_type pos = str.find_first_of(delimiters, lastPos);
while (string::npos != pos || string::npos != lastPos)
{
// Found a token, add it to the vector.
string temp = str.substr(lastPos, pos - lastPos);
tokens.push_back(strToDouble(temp));
// Skip delimiters. Note the "not_of"
lastPos = str.find_first_not_of(delimiters, pos);
// Find next "non-delimiter"
pos = str.find_first_of(delimiters, lastPos);
}
}
double strToDouble(std::string const &numberString)
{
istringstream inStream(numberString);
int pos = numberString.find_first_not_of("1234567890.", 0);
if (string::npos != pos)
{
return 0.0;
}
else
{
double doubleValue;
inStream >> doubleValue;
return doubleValue;
}
}