<<重载函数中str=buf的理解

istream & operator>>( istream & in, string & str )
{
    char buf[ string::MAX_LENGTH + 1 ];
    in >> buf;
    if( !in.fail( ) )
        str = buf; //如何理解char buf[]赋值给string对象,为什么可以这样赋值呢???
    return in;

}

答案:

需要重载针对char []的赋值运算符。即

需要有这个string& operator= ( const char* s );
有它string& operator= ( const string& str )不够的。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
完成 MyString 类的实现,使程序正确运行 #include <iostream> #include <cstring> using namespace std; class MyString { public: // 无参构造函数 MyString(); // 构造函数,传入一个C语言风格字符串 MyString(const char *s); // 拷贝构造函数 MyString(const MyString &s); // 析构函数 ~MyString(); // 将C语言风格的字符串赋值给MyString对象 MyString& operator=(const char *s); // 将一个MyString对象赋值给另一个MyString对象 MyString& operator=(const MyString &s); // 将这个MyString对象与C语言风格的字符串相连 MyString& operator+=(const char *s); // 将这个MyString对象与另一个MyString对象相连 MyString& operator+=(const MyString &s); // 返回下标为pos的字符 char& operator[](int pos); // 返回子串 [pos, pos+count) // 若请求的子串越过字符串的结尾,即count大于size() - pos,则返回的子串为[pos, size()) // 若pos不在字符串的下标范围内,返回空的MyString对象 // 若count == -1,返回子串[pos, size()) MyString substr(int pos = 0, int count = -1); // 字符串的长度 int size() const; // 返回C语言风格的字符串 const char* c_str() const; private: char *m_buf; int m_size; }; // 请在此处填写 int main() { char s[101]; cin.getline(s, 101); int pos, count; // 创建、拷贝、赋值 MyString s1(s), s2(s1), s3, s4, s5; s3 = s; s4 = s1; cout << "s1: " << s1.c_str() << endl; cout << "s2: " << s2.c_str() << endl; cout << "s3: " << s3.c_str() << endl; cout << "s4: " << s4.c_str() << endl; // 拼接 s3 += s; s4 += s1; cout << "s3 += s: " << s3.c_str() << endl; cout << "s4 += s1: " << s4.c_str() << endl; // 下标 cin >> pos; s4[pos] = 'm'; cout << "s4[pos] = 'm': " << s4.c_str() << endl; // 取子串 cin >> pos >> count; s5 = s4.substr(pos, count); cout << "s5: " << s5.c_str() << endl; // 以后还可以重载<<直接输出一个MyString对象哦 TODO return 0; }
最新发布
06-11
实现如下: ```cpp #include <iostream> #include <cstring> using namespace std; class MyString { public: // 无参构造函数 MyString() : m_buf(nullptr), m_size(0) {} // 构造函数,传入一个C语言风格字符串 MyString(const char *s) : m_buf(nullptr), m_size(0) { if (s != nullptr) { m_size = strlen(s); m_buf = new char[m_size + 1]; strcpy(m_buf, s); } } // 拷贝构造函数 MyString(const MyString &s) : m_buf(nullptr), m_size(s.m_size) { if (m_size > 0) { m_buf = new char[m_size + 1]; strcpy(m_buf, s.m_buf); } } // 析构函数 ~MyString() { delete[] m_buf; } // 将C语言风格的字符串赋值给MyString对象 MyString& operator=(const char *s) { if (s == nullptr) { delete[] m_buf; m_buf = nullptr; m_size = 0; } else { int len = strlen(s); if (len > m_size) { delete[] m_buf; m_buf = new char[len + 1]; m_size = len; } strcpy(m_buf, s); } return *this; } // 将一个MyString对象赋值给另一个MyString对象 MyString& operator=(const MyString &s) { if (this != &s) { if (s.m_buf == nullptr) { delete[] m_buf; m_buf = nullptr; m_size = 0; } else { int len = s.m_size; if (len > m_size) { delete[] m_buf; m_buf = new char[len + 1]; m_size = len; } strcpy(m_buf, s.m_buf); } } return *this; } // 将这个MyString对象与C语言风格的字符串相连 MyString& operator+=(const char *s) { int len = strlen(s); char *new_buf = new char[m_size + len + 1]; if (m_buf != nullptr) { strcpy(new_buf, m_buf); delete[] m_buf; } strcpy(new_buf + m_size, s); m_buf = new_buf; m_size += len; return *this; } // 将这个MyString对象与另一个MyString对象相连 MyString& operator+=(const MyString &s) { return (*this += s.c_str()); } // 返回下标为pos的字符 char& operator[](int pos) { return m_buf[pos]; } // 返回子串 [pos, pos+count) // 若请求的子串越过字符串的结尾,即count大于size() - pos,则返回的子串为[pos, size()) // 若pos不在字符串的下标范围内,返回空的MyString对象 // 若count == -1,返回子串[pos, size()) MyString substr(int pos = 0, int count = -1) { if (pos < 0 || pos >= m_size) { return MyString(); } if (count == -1) { count = m_size - pos; } else if (count > m_size - pos) { count = m_size - pos; } char *new_buf = new char[count + 1]; strncpy(new_buf, m_buf + pos, count); new_buf[count] = '\0'; MyString res(new_buf); delete[] new_buf; return res; } // 字符串的长度 int size() const { return m_size; } // 返回C语言风格的字符串 const char* c_str() const { return m_buf; } private: char *m_buf; int m_size; }; int main() { char s[101]; cin.getline(s, 101); int pos, count; // 创建、拷贝、赋值 MyString s1(s), s2(s1), s3, s4, s5; s3 = s; s4 = s1; cout << "s1: " << s1.c_str() << endl; cout << "s2: " << s2.c_str() << endl; cout << "s3: " << s3.c_str() << endl; cout << "s4: " << s4.c_str() << endl; // 拼接 s3 += s; s4 += s1; cout << "s3 += s: " << s3.c_str() << endl; cout << "s4 += s1: " << s4.c_str() << endl; // 下标 cin >> pos; s4[pos] = 'm'; cout << "s4[pos] = 'm': " << s4.c_str() << endl; // 取子串 cin >> pos >> count; s5 = s4.substr(pos, count); cout << "s5: " << s5.c_str() << endl; return 0; } ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值