Konwersja char * na string C++

Znam adres początkowy łańcucha (np. char* buf) i maksymalną długość int l; łańcucha (tzn. całkowita liczba znaków jest mniejsza lub równa l).

Jaki jest najprostszy sposób na uzyskanie wartości string z podanego segmentu pamięci? Innymi słowy, jak zaimplementować string retrieveString(char* buf, int l);.

EDIT : pamięć jest przeznaczona do zapisu i odczytu łańcuchów o zmiennej długości. Innymi słowy, int l;wskazuje rozmiar pamięci, a nie długość sznurek.

Author: Terry Li, 2011-12-09

5 answers

std::string str(buf, buf + l);

Lub, jeśli łańcuch już istnieje:

str.assign(buf, buf + l);

Edit: nadal nie do końca rozumiem pytanie. Ale jeśli jest to coś, co JoshG sugeruje, że chcesz maksymalnie l znaków, lub do null terminator, w zależności od tego, co nastąpi pierwsze, możesz użyć tego:

std::string str(buf, std::find(buf, buf + l, '\0'));
 18
Author: Benjamin Lindley,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2011-12-09 00:48:57
char *charPtr = "test string";
cout << charPtr << endl;

string str = charPtr;
cout << str << endl;
 7
Author: Taha,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2011-12-09 14:54:57

Wygląda na to, że zostało kilka szczegółów, ale zrobię co w mojej mocy...

Jeśli są to ciągi zakończone znakiem NUL lub pamięć jest wstępnie zerowana, możesz po prostu iterować długość segmentu pamięci aż do naciśnięcia znaku NUL (0) lub maksymalnej długości (w zależności od tego, co nastąpi wcześniej). Użyj konstruktora łańcuchowego, przekazując bufor i rozmiar określony w poprzednim kroku.

string retrieveString( char* buf, int max ) {

    size_t len = 0;
    while( (len < max) && (buf[ len ] != '\0') ) {
        len++;
    }

    return string( buf, len );

}

Jeśli powyższe Nie dotyczy, nie jestem pewien, jak określić, gdzie ciąg koniec.

 2
Author: Amish Programmer,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2011-12-08 23:25:10

Użyj konstruktora łańcucha

basic_string(const charT* s,size_type n, const Allocator& a = Allocator());

EDIT:

OK, jeśli długość łańcucha C nie jest podana jawnie, użyj ctor:

basic_string(const charT* s, const Allocator& a = Allocator());
 2
Author: chill,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2011-12-09 07:13:33
std::string str;
char* const s = "test";

str.assign(s);

string& assign (const char* s); => signature FYR

Reference / S here .

 0
Author: parasrish,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2018-02-15 05:46:08