Frage

Ich versuche zur Zeit eine Zeichenfolge ( „0,1“) zu nehmen und es zu einem Doppel unter Verwendung von C ++ konvertiert in Xcode auf 10,6 mit gcc4.2.

Ich verwende eine Funktion, die ich von eine andere Frage , aber wenn ich versuche, um die Funktion zu nutzen, meine Eingabe nach gDB ist (string) "0,1", aber meine Ausgabe ist (double) 2.1220023981051542e-314.

Hier ist mein Schnipsel direkt aus dem Code kopiert:

double strToDouble(std::string const &numberString)
{
    istringstream inStream(numberString);
    double doubleValue;
    inStream >> doubleValue;
    if(!(inStream && (inStream >> std::ws).eof()))
    {
        return 0.0;  
    }

    return doubleValue;
};

Ich bin mit C ++ statt Obj-C, wie es wahrscheinlich irgendwann auf einem * nichts oder Windows-Rechnern werden muß zusammengestellt werden.

Ich bin von Natur aus ein PHP-Programmierer, aber eine gewisse Anzahl haben Knirschen Ich brauche bis zu beschleunigen, so dass ich es in einer kompilierten Sprache zu tun. Es ist seit Universität eine lange Zeit mit einer niedrigeren Ebene language = P handelt. Also, wenn jemand eine Idee hat, wäre es sehr zu schätzen, ...

Drew J. Sonne.

Voll Code:

#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;
}
War es hilfreich?

Lösung

Dies kann durch STL Debug-Modus verursacht werden. Entfernen Sie die _GLIBCXX_DEBUG Makros in Ihrem Ziel der Präprozessormakros Build-Einstellung.

C ++ Debug baut in Snow Leopard X-Kodex pleite

Andere Tipps

Warum nicht verwenden atof () statt? Link

Ich kann nichts falsch mit Ihrem Code-Fragmente sehen. Können Sie den Code anzeigen, die Sie verwenden, um Ausgang das Ergebnis

Als Myles vorgeschlagen, atof () könnte einfacher sein. Sie können durch den Aufruf der c_str () Member-Funktion ein String-Objekt zu einem C-String konvertieren:

double result = atof(inputString.c_str()); // 0.0 if a double couldn't be read

Hier einige weitere Informationen: 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;
}

Der obige Code gibt mir die folgende Ausgabe:
    -2.36907e-39
    0,1

Sind Sie zufällig den Wert von double Überprüfung vor der Rückkehr?


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;
}

Ausgabe:
    templat = 0,1
    latDouble = 0,1
    templat = 0,4
    latDouble = 0,4

Können Sie den Code mit diesen Änderungen ausführen (Sie wurden die int-Parameter in der Ctor nicht verwenden, so dass ich es einfach entfernt). Ich lief auch Ihren Code valgrind ohne Fehler verwenden (nur Speicherlecks).

Sieht aus wie Sie nicht auf Ihre implemetion Frage beantworten bekommen. Andere haben vorgeschlagen, atof () zu verwenden. Wenn Sie möchten, dass das Problem in Ihrer UMSETZUNG lösen, dann am folgenden Code suchen. Stattdessen Vektor Verfahren zur Explosion zu haben, setzte Vektor redundanten Code zu vermeiden. Ich änderte auch den Code in 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;
  }
}
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top