을 만들 std::string 에서 int8_t 배열

0

질문

일부에서 코드 int8_t[] 형식을 사용하는 대신 char[].

int8_t title[256] = {'a', 'e', 'w', 's'};
std::string s(title); // compile error: no corresponding constructor

어떻게 안전하고 올바르게 만들기 std::string 에서 그것은?

때 나는 할 것입니다 cout << s; 내가 원하는 인쇄 aewschar[] 입력 전달되었습니다.

c++ casting char integer
2021-11-23 15:34:12
2

최고의 응답

2

여기에 당신은 당신

int8_t title[256] = { 'a', 'e', 'w', 's' };
std::string s( reinterpret_cast<char *>( title ) );
std::cout << s << '\n';

또는 사용할 수 있습니다 또한

std::string s( reinterpret_cast<char *>( title ), 4 );
2021-11-23 15:45:55

소리 같은 나쁜 생각지 않고 명시적 null 터미네이터에 배열입니다.
dave

@데이브 및 당신은 왜 결정이 없는 null 을 종료하는 캐릭터가?
Vlad from Moscow

이 있어야 252null 터미네이터에서는 배열입니다. :-)
Ted Lyngmo

@데이브 나도 같은 일을 한 다음 기억하는 모든 누락된 이니셜라이저로 설정 0그래서 그것 252null 터미네이터습니다.
NathanOliver

아 권리,보지 않았 256 크기입니다. 는 확인 후에서는 이례
dave
1

std::string 다음과 같은 다른 컨테이너를 사용하여 생성할 수 있습 쌍의 이터레이터입니다. 이 생성자를 사용 암시적 변환을 사용할 수 있는 경우,등의 변환 int8_t 하기 char.

int8_t title[256] = {'a', 'e', 'w', 's'};
std::string s(std::begin(title), std::end(title));

이 솔루션은 사본을 전체 배열,포함하여 사용하지 않는 바이트 단위이다. 는 경우에는 배열은 종종 훨씬 더 큰 것보다 그것이 필요하다,당신은 당신이 볼 수있는 null 터미네이터 대

int8_t title[256] = {'a', 'e', 'w', 's'};
auto end = std::find(std::begin(title), std::end(title), '\0');
std::string s(std::begin(title), end);
2021-11-23 15:38:17

다른 언어로

이 페이지는 다른 언어로되어 있습니다

Русский
..................................................................................................................
Italiano
..................................................................................................................
Polski
..................................................................................................................
Română
..................................................................................................................
हिन्दी
..................................................................................................................
Français
..................................................................................................................
Türk
..................................................................................................................
Česk
..................................................................................................................
Português
..................................................................................................................
ไทย
..................................................................................................................
中文
..................................................................................................................
Español
..................................................................................................................
Slovenský
..................................................................................................................