質問

私は現在、(「0.1」)の文字列を取り、gcc4.2で10.6にXcodeでの二重使用してC ++に変換しようとしています。

私は使用していますのが、私は機能を使用しようと、GDBによると、私の入力がある(文字列)「0.1」が、私の出力は(ダブル)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;
};
それはおそらく最終的には* nixのか、Windowsマシン上でコンパイルする必要がありますように、

私は、かなりのObj-CよりもC ++を使用しています。

私は自然にPHPプログラマーだけど、私はスピードアップする必要がバリバリいくつかの番号を持っているので、私はコンパイル言語でそれをやっています。これは、低レベルの言語= Pを扱う大学以来の長い時間がかかりました。誰もがアイデアを持っているのであれば、それは非常に高く評価されるだろう...

ドリューJ.ゾンネ。

全コード:

#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マクロを削除します。

C ++デバッグ

他のヒント

私はあなたのコードの断片と間違って何も見ることができません。あなたは、の出力の結果

に使用しているコードを表示することができます
マイルズが示唆したように、

グループ、atof()は単純かもしれません。あなたはc_str()メンバ関数を呼び出すことにより、Cの文字列に文字列オブジェクトに変換することができます:

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のを使用してコードを実行しました。

あなたはimplemetionの問題への答えを取得していないように見えます。その他atofを使用することを示唆しています()。あなたはimplementionで問題を解決したい場合は、以下のコードを見てください。代わりの方法を爆発するベクトルを有していると、冗長なコードを避けるために、ベクターを置きます。私も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;
  }
}
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top