鱼C论坛

 找回密码
 立即注册
查看: 2802|回复: 11

类型转换问题 求助

[复制链接]
发表于 2022-9-29 18:05:37 | 显示全部楼层 |阅读模式
10鱼币
做了一个转换类, 但是比如说是字符串 中间有空格 就会在空格前截断, 但是如果不用模板类声明 w/stringstream 就是正常的

  1. //根据数据类型 自动选择 stream 的类型是 stringstream 还是 wstringstream
  2. //比如说函数的返回值是wstring, CStringW, wchar_t*, 那么 stream的类型应该是wstringstream
  3. //      那么否则 是string CStringA, char* stream的类型应该是stringstream

  4. #include <string>
  5. #include <iostream>
  6. #include <sstream>

  7. template<typename T>
  8. struct convert_string {
  9.         typedef std::stringstream type;
  10. };

  11. template<>
  12. struct convert_string<wchar_t*> {
  13.         typedef std::wstringstream type;
  14. };

  15. template<>
  16. struct convert_string<const wchar_t*> {
  17.         typedef std::wstringstream type;
  18. };

  19. template<>
  20. struct convert_string<std::wstring > {
  21.         typedef std::wstringstream type;
  22. };

  23. template<>
  24. struct convert_string<const std::wstring > {
  25.         typedef std::wstringstream type;
  26. };

  27. template <typename TRet, typename TParam>
  28. inline TRet Convert(const TParam& t)
  29. {
  30.         typename convert_string<TRet>::type stream;
  31.         TRet tRet;
  32.         stream << t;
  33.         stream >> tRet;
  34.         return tRet;
  35. }

  36. int main()
  37. {
  38.         using namespace std;
  39.         const char* pmsg = "hello world";
  40.         std::stringstream a;
  41.         std::wstringstream b;
  42.         auto ret = Convert<std::string, const char*>(pmsg);
  43.         auto nex = Convert<std::wstring>(L"WH AT");
  44.         std::cout << ret;
  45.         Convert<string>("12 34");
  46.         Convert<string>("hello world");
  47.         Convert<string>(L"1234");
  48.         Convert<int>("1234");
  49.         Convert<int>(L"1234");
  50.         Convert<wstring>("2234");
  51.         Convert<wstring>(L"1234");
  52.         Convert<string>(3.15);
  53.         Convert<wstring>(L"1234");
  54.         return 0;
  55. }
复制代码

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2022-9-29 20:47:24 | 显示全部楼层
没看懂你的问题,把两个代码都贴出来
一个没问题的和一个有问题的,然后说明你的问题
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2022-9-30 08:13:00 | 显示全部楼层
人造人 发表于 2022-9-29 20:47
没看懂你的问题,把两个代码都贴出来
一个没问题的和一个有问题的,然后说明你的问题

就是下面 Convert<string>("12 34"); 只会转换12,  34丢了
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2022-9-30 08:40:38 | 显示全部楼层
pediyzhi 发表于 2022-9-30 08:13
就是下面 Convert("12 34"); 只会转换12,  34丢了

这不是很正常么?
你的意思是 你的另外一个代码可以把12,34都转换了?
贴出来看看
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2022-9-30 08:41:48 | 显示全部楼层
pediyzhi 发表于 2022-9-30 08:13
就是下面 Convert("12 34"); 只会转换12,  34丢了

但是如果不用模板类声明 w/stringstream 就是正常的
你把 不用模板类声明 w/stringstream 的版本发出来看看
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2022-10-2 07:29:03 | 显示全部楼层
人造人 发表于 2022-9-30 08:41
但是如果不用模板类声明 w/stringstream 就是正常的
你把 不用模板类声明 w/stringstream 的版本发出来 ...

不是, 我的意思是, 如果是字符串的话Hello Word 也只会有Hello
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2022-10-2 10:34:45 | 显示全部楼层
.
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2022-10-2 11:18:36 | 显示全部楼层
pediyzhi 发表于 2022-10-2 07:29
不是, 我的意思是, 如果是字符串的话Hello Word 也只会有Hello

看不懂你的问题
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2022-10-2 11:20:04 | 显示全部楼层
pediyzhi 发表于 2022-10-2 07:29
不是, 我的意思是, 如果是字符串的话Hello Word 也只会有Hello

你不是说 不用模板类声明 w/stringstream 就是正常的 么?
你把这个正常的代码发出来看看
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2022-10-2 17:44:09 | 显示全部楼层
人造人 发表于 2022-10-2 11:20
你不是说 不用模板类声明 w/stringstream 就是正常的 么?
你把这个正常的代码发出来看看

好奇怪. 我现在正常的也不正常了.
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2023-4-6 21:51:47 | 显示全部楼层
题在第41行
stream流在传递给tRet的时候,默认只传递第一个空格前的内容,可以考虑使用循环
借用一个临时变量tf 或许可以这么写:
  1. TRet tf;
  2. while(stream >> tf) {
  3.     tRet += tf;
  4.     tRet += ' '; //保留空格
  5. }
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2023-4-7 20:10:43 | 显示全部楼层
这段代码有一个问题,它在处理带空格的字符串时会截断空格之前的部分。这是因为默认情况下`operator>>`会停止在空格处。要解决这个问题,可以使用`std::getline()`函数而不是`operator>>`来读取输入,因为它可以读取整行文本,包括空格。

以下是修改后的示例代码:


  1. #include <string>
  2. #include <iostream>
  3. #include <sstream>

  4. template<typename T>
  5. struct convert_string {
  6.         typedef std::stringstream type;
  7. };

  8. template<>
  9. struct convert_string<wchar_t*> {
  10.         typedef std::wstringstream type;
  11. };

  12. template<>
  13. struct convert_string<const wchar_t*> {
  14.         typedef std::wstringstream type;
  15. };

  16. template<>
  17. struct convert_string<std::wstring > {
  18.         typedef std::wstringstream type;
  19. };

  20. template<>
  21. struct convert_string<const std::wstring > {
  22.         typedef std::wstringstream type;
  23. };

  24. template <typename TRet, typename TParam>
  25. inline TRet Convert(const TParam& t)
  26. {
  27.         typename convert_string<TRet>::type stream;
  28.         TRet tRet;
  29.         // 读取整行文本
  30.         std::getline(stream, tRet);
  31.         return tRet;
  32. }

  33. int main()
  34. {
  35.         using namespace std;
  36.         const char* pmsg = "hello world";
  37.         std::stringstream a;
  38.         std::wstringstream b;
  39.         auto ret = Convert<std::string, const char*>(pmsg);
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-4-20 16:42

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表