如何将 std::wstring 与 std::istringstream 一起使用?

发布于 2024-08-12 13:30:47 字数 782 浏览 1 评论 0原文

我正在尝试编写一个模板函数,它将从给定的字符串中提取给定数据类型的值。我想出了这样的方法:

   template<class T>
    static T getValue(const CString& val_in)
    {
        std::wstring value = val_in;
        std::istringstream iss;
        iss.str(value);

        T val = T();
        iss>>val;
        return val;
    }

但这会给 iss.str(value) 语句带来以下错误。

错误 C2664:'无效 std::basic_istringstream<_Elem,_Traits,_Alloc>::str(const std::basic_string<_Elem,_Traits,_Ax> &)' : 无法将参数 1 转换为 'std::wstring' 到 'const std::basic_string<_Elem,_Traits,_Ax> &'

所以基本上, std::istringstream 仅接受 std::string 。我认为可能有一个 std::wistringstream 但似乎没有可用的。有什么线索我该怎么做吗?

I am trying to write a template function which will extract the value of the given datatype from the given string. I came up with something like this:

   template<class T>
    static T getValue(const CString& val_in)
    {
        std::wstring value = val_in;
        std::istringstream iss;
        iss.str(value);

        T val = T();
        iss>>val;
        return val;
    }

But this gives the following error for the iss.str(value) statement.

error C2664: 'void
std::basic_istringstream<_Elem,_Traits,_Alloc>::str(const
std::basic_string<_Elem,_Traits,_Ax>
&)' : cannot convert parameter 1 from
'std::wstring' to 'const
std::basic_string<_Elem,_Traits,_Ax>
&'

So basically, std::istringstream is accepting only std::string . I thought there may be a std::wistringstream but there doesn't seem to be one available. Any clues how can I do it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

失去的东西太少 2024-08-19 13:30:47

我的编译器有 wistringstream —— 这就是全部:

typedef basic_istringstream; wistringstream;

My compiler has wistringstream -- this is all it is:

typedef basic_istringstream<wchar_t> wistringstream;

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文