สร้างเชื้อโรคที่ติดต่ดทางเพศ::ข้อความจาก 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; ฉันอยากให้มันพิมพ์ aewsเหมือนว่า char[] ประเภทนผ่านไป constructor.

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

ฟังดูเหมือนเป็นความคิดที่แย่มาโดยไม่มี explicit โพรโทคอล aimcomment หุ่นสังหาอยู่ในอาเรย์.
dave

@เดฟและทำไมคุณต้องตัดสินใจว่าไม่มีโพรโทคอล aimcomment terminating ตัวละคร?
Vlad from Moscow

มันควรจะ 252 โพรโทคอล aimcomment terminators อยู่ในนั้นอาเรย์. :-)
Ted Lyngmo

@เดฟผมคิดว่าสิ่งเดียวกันและจากนั้นก็นึกขึ้นได้ว่าทั้งหมดมันหายไป initializers เป็นตั้ง 0ดังนั้นมันมี 252 โพรโทคอล aimcomment terminators อยู่ในนั้น
NathanOliver

อ่าใช่ก็ไม่เห็น 256 ยขนาดด้วย ไม่เป็นไรแล้วในกรณีนี้
dave
1

std::string เหมือนกัน containers สามารถเป็ตูใช้คู่ iterators. นี่ constructor จะใช้ที่แยกกำหนด conversions ถ้ามีอย่างเช่นการเปลี่ยน int8_t ต้อง char.

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

โปรดจำไว้ว่ามันแก้ปัญหาจะคัดลอกทั้งอาเรย์รวมทั้งที่ไม่ได้ใช้อมู. ถ้าอาเรย์คือบ่อยมากมีขนาดใหญ่กว่ามันต้องการคุณสามารถฟังนะสำหรับโพรโทคอล aimcomment หุ่นสังหาแทนที่จะเป็น

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ý
..................................................................................................................

ดังอยู่ในนี้หมวดหมู่

ดังคำถามอยู่ในนี้หมวดหมู่