gpt4 book ai didi

wstring - 比较 std::wstring 类型并表示 ChaiScript 中的 wstring 文字

转载 作者:行者123 更新时间:2023-12-02 04:48:34 27 4
gpt4 key购买 nike

我想像下面的 c++ 代码一样使用 std::wstring 类型编写 ChaiScript 代码。

#include <iostream>

int testfunc(std::wstring s, std::wstring t)
{
if(s==t)
{
std::cout << "1" << std::endl;
}

if(s[1]==t[1])
{
std::cout << "2" << std::endl;
}

if(s==L"aaaa")
{
std::cout << "3" << std::endl;
}

if(s[1]==L'b')
{
std::cout << "4" << std::endl;
}

return 5;
}

int main()
{
std::cout << testfunc(std::wstring(L"abcd"), std::wstring(L"abbb"));

return 0;
}

D:\TestWork\test_chaiscript>t6
2
4
5

比较 std::wstring 类型的实例是好的。

#include <iostream>
#include <chaiscript/chaiscript.hpp>
#include <chaiscript/chaiscript_stdlib.hpp>
#include <chaiscript/dispatchkit/bootstrap_stl.hpp>

int main()
{
chaiscript::ChaiScript chai(chaiscript::Std_Lib::library());

chai.add(chaiscript::bootstrap::standard_library::string_type<std::wstring>("wstring"));

std::cout << chai.eval<std::function<int (std::wstring, std::wstring)> >(
"fun(s, t){"
" if(s==t){"
" print(\"1\");"
" }"
" return 3;"
"}"
)(std::wstring(L"abcd"), std::wstring(L"abaa"));

return 0;
}

D:\TestWork\test_chaiscript>t5
3

比较 wchar_t 类型的实例是行不通的。

是否必须添加比较运算符方法?

        "   if(s[1]==t[1]){"
" print(\"2\");"
" }"

D:\TestWork\test_chaiscript>t5
terminate called after throwing an instance of 'chaiscript::exception::eval_error'
what(): Error: "Error with numeric operator calling: =="

将 std::wstring 类型的实例与字符串类型的文字进行比较是行不通的。无法输入wstring类型的文字。

是否可以在ChaiScript中输入wstring类型的文字?

        "   if(s==\"aaaa\"){"
" print(\"2\");"
" }"

D:\TestWork\test_chaiscript>t5
terminate called after throwing an instance of 'chaiscript::exception::eval_error'
what(): Error: "Can not find appropriate '==' operator." With parameters: (wstring, const string)

将 wchar_t 类型的实例与 wchar_t 类型的文字进行比较是行不通的。我无法输入 wchat_t 类型的文字。

是否可以在ChaiScript中输入wchat_t类型的文字?

        "   if(s[1]=='b'){"
" print(\"2\");"
" }"

D:\TestWork\test_chaiscript>t5
terminate called after throwing an instance of 'chaiscript::exception::eval_error'
what(): Error: "Error with numeric operator calling: =="

最佳答案

是的,您需要告诉 ChaiScript 一些关于 wchar_t 的信息,例如 == 运算符。例如:

chai.add(
chaiscript::fun<bool (wchar_t, wchar_t)>(
[](wchar_t lhs, wchar_t rhs) { return lhs == rhs; }
), "=="
);

关于wstring - 比较 std::wstring 类型并表示 ChaiScript 中的 wstring 文字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30838757/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com