string buf="qqqq";printf("%s \n",buf.c_str); 与std::cout<<buf ;输出的区别
时间: 2023-08-07 09:04:08 浏览: 62
printf("%s \n",buf.c_str); 和 std::cout<<buf; 输出结果的区别在于格式化输出的方式和输出的位置。
在第一种情况下,使用 printf 函数进行格式化输出时,使用了格式化字符串 "%s" 来表示输出一个字符串。而 buf.c_str() 是将 C++ 的 std::string 对象 buf 转换为 C 风格的字符串,以便与格式化字符串相匹配。这种方式需要手动指定输出的格式,并且需要使用 c_str() 函数来获取 C 风格的字符串。
在第二种情况下,使用 std::cout 进行输出时,不需要进行格式化,直接使用运算符重载来输出字符串对象 buf。std::cout 是 C++ 的标准输出流对象,可以直接输出 C++ 的字符串对象。
总的来说,printf("%s \n",buf.c_str); 是使用 C 的方式进行格式化输出,需要手动指定格式并转换为 C 风格的字符串;而 std::cout<<buf; 是使用 C++ 的方式直接输出字符串对象,不需要进行格式化和转换。
相关问题
std::string yb_data = msg_data["data"]; std::cout << "函数调用" << "data: " << yb_data << std::endl; //获取当前时间 auto now = std::chrono::system_clock::now(); std::time_t now_c = std::chrono::system_clock::to_time_t(now); // 将时间转换为本地时间 std::tm* local_now = std::localtime(&now_c);能否获得更具体的时间
是的,您可以使用`std::chrono::milliseconds`获取更具体的时间,它可以表示从1970年1月1日零时起的毫秒数。您可以使用以下代码获取当前的毫秒数:
```
auto now = std::chrono::system_clock::now();
auto now_ms = std::chrono::time_point_cast<std::chrono::milliseconds>(now);
auto value = now_ms.time_since_epoch().count();
```
然后,您可以使用`std::gmtime`或`std::localtime`将毫秒数转换为本地或UTC时间,并使用`std::strftime`格式化输出。例如,以下代码将毫秒数转换为本地时间并以指定的格式打印:
```
auto time_ms = std::chrono::milliseconds(value);
auto local_time = std::chrono::time_point_cast<std::chrono::system_clock::duration>(time_ms);
auto local_tm = std::localtime(std::addressof(std::mktime(local_time)));
char buf[100];
std::strftime(buf, sizeof(buf), "%Y-%m-%d %H:%M:%S", local_tm);
std::cout << buf << "." << (value % 1000) << std::endl;
```
完成 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; }
实现如下:
```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;
}
```
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)