790 Stimmen

Verkettung eines std::string und eines int

Ich dachte, das wäre ganz einfach, aber es gibt einige Schwierigkeiten. Wenn ich

std::string name = "John";
int age = 21;

Wie kombiniere ich sie, um eine einzige Zeichenfolge zu erhalten? "John21" ?

1 Stimmen

Herb Sutter hat einen guten Artikel zu diesem Thema: "Die Streichinstrumentenbauer von Manor Farm" . Er deckt Boost::lexical_cast , std::stringstream , std::strstream (die veraltet ist) und sprintf vs. snprintf .

1 Stimmen

Lassen Sie mich dies noch ergänzen: Ich habe 'str = "hi"; str += 5; cout << str;' ausprobiert und keinen Effekt gesehen. Es stellte sich heraus, dass dies operator+=(char) aufruft und ein nicht druckbares Zeichen hinzufügt.

2voto

David G Punkte 90243

Hier ist eine Implementierung des Anhängens eines int an einen String unter Verwendung der Parsing- und Formatierungsfacetten der IOStreams-Bibliothek.

#include <iostream>
#include <locale>
#include <string>

template <class Facet>
struct erasable_facet : Facet
{
    erasable_facet() : Facet(1) { }
    ~erasable_facet() { }
};

void append_int(std::string& s, int n)
{
    erasable_facet<std::num_put<char,
                                std::back_insert_iterator<std::string>>> facet;
    std::ios str(nullptr);

    facet.put(std::back_inserter(s), str,
                                     str.fill(), static_cast<unsigned long>(n));
}

int main()
{
    std::string str = "ID: ";
    int id = 123;

    append_int(str, id);

    std::cout << str; // ID: 123
}

1voto

Tom Ritter Punkte 97450

Allgemeine Antwort: itoa()

Das ist schlecht. itoa ist, wie bereits erwähnt, nicht standardisiert aquí .

1voto

Reda Lahdili Punkte 1533

Es gibt eine Funktion, die ich geschrieben habe, die die int-Zahl als Parameter nimmt und in ein String-Literal konvertiert. Diese Funktion ist von einer anderen Funktion abhängig, die eine einzelne Ziffer in ihr Zeichenäquivalent umwandelt:

char intToChar(int num)
{
    if (num < 10 && num >= 0)
    {
        return num + 48;
        //48 is the number that we add to an integer number to have its character equivalent (see the unsigned ASCII table)
    }
    else
    {
        return '*';
    }
}

string intToString(int num)
{
    int digits = 0, process, single;
    string numString;
    process = num;

    // The following process the number of digits in num
    while (process != 0)
    {
        single  = process % 10; // 'single' now holds the rightmost portion of the int
        process = (process - single)/10;
        // Take out the rightmost number of the int (it's a zero in this portion of the int), then divide it by 10
        // The above combination eliminates the rightmost portion of the int
        digits ++;
    }

    process = num;

    // Fill the numString with '*' times digits
    for (int i = 0; i < digits; i++)
    {
        numString += '*';
    }

    for (int i = digits-1; i >= 0; i--)
    {
        single = process % 10;
        numString[i] = intToChar ( single);
        process = (process - single) / 10;
    }

    return numString;
}

CodeJaeger.com

CodeJaeger ist eine Gemeinschaft für Programmierer, die täglich Hilfe erhalten..
Wir haben viele Inhalte, und Sie können auch Ihre eigenen Fragen stellen oder die Fragen anderer Leute lösen.

Powered by:

X