2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

charとstd::string

Last updated at Posted at 2018-12-18

charの配列を、std::stringに代入したときの挙動を調べたので、置いておきます。

サイズ指定が無いcharからstd::stringへの代入は、0が出てきたら文字列が途切れる。
サイズ指定すれば、0が含まれる文字であっても、ちゃんと値を持っている。
std::stringstreamのstr()が返す内容も、0が含まれていても、ちゃんと値を持っている。

b.cpp
# include <string>
# include <iostream>
# include <sstream>

int main(int argc, char **argv) {
        char text[] = {97, 0, 98, 0, 99, 0};

        std::cout << "text output." << std::endl;
        std::cout << text << std::endl;
        std::cout << std::endl;

        std::string s1 = text;

        std::cout << "s1 output." << std::endl;
        std::cout << s1 << "\t" << s1.size() << std::endl;
        std::cout << std::endl;

        std::string s2(text, sizeof(text) / sizeof(char));

        std::cout << "s2 output." << std::endl;
        std::cout << s2 << "\t" << s2.size() << std::endl;
        std::cout << std::endl;

        for (size_t i = 0, ei = s2.size(); i < ei; ++i) {
                std::cout << s2[i]  << "\t" << static_cast<int>(s2[i]) << std::endl;
        }

        std::cout << std::endl;

        char text2[s2.size()];
        std::char_traits<char>::copy(text2, s2.c_str(), s2.size());

        std::cout << "text2 output." << std::endl;
        std::cout << text2 << std::endl;
        std::cout << std::endl;

        for (size_t i = 0, ei = sizeof(text2) / sizeof(char); i < ei; ++i) {
                std::cout << text2[i] << "\t" << static_cast<int>(text2[i]) << std::endl;
        }

        std::cout << std::endl;

        std::stringstream ss;
        ss.write(text, sizeof(text) / sizeof(char));

        std::string s3 = ss.str();

        std::cout << "s3 output." << std::endl;
        std::cout << s3 << "\t" << s3.size() << std::endl;
        std::cout << std::endl;

        for (size_t i = 0, ei = s3.size(); i < ei; ++i) {
                std::cout << s3[i]  << "\t" << static_cast<int>(s3[i]) << std::endl;
        }
}

[todanano@localhost ~]$ g++ --version
g++ (GCC) 4.8.5 20150623 (Red Hat 4.8.5-36)
Copyright (C) 2015 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

[todanano@localhost ~]$ g++ b.cpp
[todanano@localhost ~]$ ./a.out
text output.
a

s1 output.
a       1

s2 output.
abc     6

a       97
        0
b       98
        0
c       99
        0

text2 output.
a

a       97
        0
b       98
        0
c       99
        0

s3 output.
abc     6

a       97
        0
b       98
        0
c       99
        0

[todanano@localhost ~]$
2
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?