Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/159.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 通过c+处理不同字符串编码的正确方法是什么+;主字符**args?_C++_Linux_String_Encoding_Utf 8 - Fatal编程技术网

C++ 通过c+处理不同字符串编码的正确方法是什么+;主字符**args?

C++ 通过c+处理不同字符串编码的正确方法是什么+;主字符**args?,c++,linux,string,encoding,utf-8,C++,Linux,String,Encoding,Utf 8,我需要一些澄清 问题是我有一个用C++编写的Windows程序,它使用WWME的Windows特定函数,它接受W查尔特**作为其ARG。因此,有机会将您喜欢的任何命令行参数传递给此类程序:例如,中文符号、日文符号等 老实说,我没有关于这个函数通常使用的编码的信息。可能是utf-32,甚至utf-16。 因此,问题是: 用标准的main函数实现这一点的方法不是windows特有的,而是unix/linux?我的第一个想法是使用utf-8编码的输入字符串,并指定某种语言环境 有人能举一个这样的主要

我需要一些澄清

问题是我有一个用C++编写的Windows程序,它使用WWME的Windows特定函数,它接受W查尔特**作为其ARG。因此,有机会将您喜欢的任何命令行参数传递给此类程序:例如,中文符号、日文符号等

老实说,我没有关于这个函数通常使用的编码的信息。可能是utf-32,甚至utf-16。 因此,问题是:

  • 用标准的main函数实现这一点的方法不是windows特有的,而是unix/linux?我的第一个想法是使用utf-8编码的输入字符串,并指定某种语言环境

  • 有人能举一个这样的主要功能的简单例子吗?一个std::字符串如何保存一个中文符号

  • 当我们像这样访问每个字符(字节):string_object[i]时,我们能像往常一样使用utf-8编码的、包含在std::strings中的中文符号吗
简而言之:

int main(int argc,char**argv){
setlocale(LC_CTYPE,“”);
// ...
}

然后你使用。您仍然可以使用普通的
std::string
存储多字节字符串,但请注意其中的字符可能跨越多个数组单元格。成功设置区域设置后,您还可以使用宽流(wcin、wcout、wcerr)从标准流读取和写入宽字符串。

简而言之:

int main(int argc,char**argv){
setlocale(LC_CTYPE,“”);
// ...
}

然后你使用。您仍然可以使用普通的
std::string
存储多字节字符串,但请注意其中的字符可能跨越多个数组单元格。成功设置区域设置后,您还可以使用宽流(wcin、wcout、wcerr)从标准流中读取和写入宽字符串。

1)使用linux,您将获得标准
main()
和标准
char
。它将使用UTF-8编码。因此,特定于中文的字符将包含在多字节编码的字符串中。
***编辑:**抱歉,是:您必须设置默认的“”语言环境以及。*

2) 所有经典的
main()
示例都是很好的示例。如上所述,特定于中文的字符将包含在多字节编码的字符串中。因此,如果您使用默认的UTF-8语言环境输入这样一个字符串,cout sream将解释特殊的UTF8编码序列,知道它必须在每个序列的2到6之间进行转换才能生成中文输出

3) 您可以像往常一样对字符串进行操作。但是,如果无法确定字符串长度,则会出现一些问题,例如:内存(例如:3个字节)和用户看到的字符(例如:仅1个)之间存在差异。如果使用指针向前或向后移动,则相同。您必须确保正确解释多字节编码,以避免输出无效编码

你可能会对它感兴趣

解释UTF-8多字节编码的逻辑。从本文中,您将了解到,任何字符
u
都是多字节编码字符,如果:

( ((u & 0xE0) == 0xC0)
       || ((u & 0xF0) == 0xE0)
       || ((u & 0xF8) == 0xF0)
       || ((u & 0xFC) == 0xF8)
       || ((u & 0xFE) == 0xFC) ) 
后跟一个或多个字符,例如:

((u & 0xC0) == 0x80)
所有其他字符均为ASCII字符(即非多字节)

1)使用linux,您将获得标准的
main()
,以及标准的
char
。它将使用UTF-8编码。因此,特定于中文的字符将包含在多字节编码的字符串中。
***编辑:**抱歉,是:您必须设置默认的“”语言环境以及。*

2) 所有经典的
main()
示例都是很好的示例。如上所述,特定于中文的字符将包含在多字节编码的字符串中。因此,如果您使用默认的UTF-8语言环境输入这样一个字符串,cout sream将解释特殊的UTF8编码序列,知道它必须在每个序列的2到6之间进行转换才能生成中文输出

3) 您可以像往常一样对字符串进行操作。但是,如果无法确定字符串长度,则会出现一些问题,例如:内存(例如:3个字节)和用户看到的字符(例如:仅1个)之间存在差异。如果使用指针向前或向后移动,则相同。您必须确保正确解释多字节编码,以避免输出无效编码

你可能会对它感兴趣

解释UTF-8多字节编码的逻辑。从本文中,您将了解到,任何字符
u
都是多字节编码字符,如果:

( ((u & 0xE0) == 0xC0)
       || ((u & 0xF0) == 0xE0)
       || ((u & 0xF8) == 0xF0)
       || ((u & 0xFC) == 0xF8)
       || ((u & 0xFE) == 0xFC) ) 
后跟一个或多个字符,例如:

((u & 0xC0) == 0x80)

所有其他字符均为ASCII字符(即非多字节)

免责声明:提供的所有中文单词

1)只需使用normal
std::string
正常进行即可。
std::string
可以保存任何字符编码,参数处理是简单的模式匹配。因此,在安装了中文版本程序的中文计算机上,它所需要做的就是将标志的中文版本与用户输入的内容进行比较

2)例如:

#include <string>
#include <vector>
#include <iostream>

std::string arg_switch = "开关";
std::string arg_option = "选项";
std::string arg_option_error = "缺少参数选项";

int main(int argc, char* argv[])
{
    const std::vector<std::string> args(argv + 1, argv + argc);

    bool do_switch = false;
    std::string option;

    for(auto arg = args.begin(); arg != args.end(); ++arg)
    {
        if(*arg == "--" + arg_switch)
            do_switch = true;
        else if(*arg == "--" + arg_option)
        {
            if(++arg == args.end())
            {
                // option needs a value - not found
                std::cout << arg_option_error << '\n';
                return 1;
            }
            option = *arg;
        }
    }

    std::cout << arg_switch << ": " << (do_switch ? "on":"off") << '\n';
    std::cout << arg_option << ": " << option << '\n';

    return 0;
}
./program --开关 --选项 wibble
开关: on
选项: wibble
输出:

#include <string>
#include <vector>
#include <iostream>

std::string arg_switch = "开关";
std::string arg_option = "选项";
std::string arg_option_error = "缺少参数选项";

int main(int argc, char* argv[])
{
    const std::vector<std::string> args(argv + 1, argv + argc);

    bool do_switch = false;
    std::string option;

    for(auto arg = args.begin(); arg != args.end(); ++arg)
    {
        if(*arg == "--" + arg_switch)
            do_switch = true;
        else if(*arg == "--" + arg_option)
        {
            if(++arg == args.end())
            {
                // option needs a value - not found
                std::cout << arg_option_error << '\n';
                return 1;
            }
            option = *arg;
        }
    }

    std::cout << arg_switch << ": " << (do_switch ? "on":"off") << '\n';
    std::cout << arg_option << ": " << option << '\n';

    return 0;
}
./program --开关 --选项 wibble
开关: on
选项: wibble
3)编号

对于UTF-8/UTF-16数据,我们需要使用特殊的库,如


对于逐字符处理,您需要使用或转换为UTF-32。

免责声明:提供的所有中文单词

1)只需使用normal
std::string
正常进行即可。
std::string
可以保存任何字符编码,参数处理是简单的模式匹配。因此,在安装了中文版本程序的中文计算机上,它所需要做的就是将标志的中文版本与用户输入的内容进行比较

2)例如:

#include <string>
#include <vector>
#include <iostream>

std::string arg_switch = "开关";
std::string arg_option = "选项";
std::string arg_option_error = "缺少参数选项";

int main(int argc, char* argv[])
{
    const std::vector<std::string> args(argv + 1, argv + argc);

    bool do_switch = false;
    std::string option;

    for(auto arg = args.begin(); arg != args.end(); ++arg)
    {
        if(*arg == "--" + arg_switch)
            do_switch = true;
        else if(*arg == "--" + arg_option)
        {
            if(++arg == args.end())
            {
                // option needs a value - not found
                std::cout << arg_option_error << '\n';
                return 1;
            }
            option = *arg;
        }
    }

    std::cout << arg_switch << ": " << (do_switch ? "on":"off") << '\n';
    std::cout << arg_option << ": " << option << '\n';

    return 0;
}
./program --开关 --选项 wibble
开关: on
选项: wibble
输出:

#include <string>
#include <vector>
#include <iostream>

std::string arg_switch = "开关";
std::string arg_option = "选项";
std::string arg_option_error = "缺少参数选项";

int main(int argc, char* argv[])
{
    const std::vector<std::string> args(argv + 1, argv + argc);

    bool do_switch = false;
    std::string option;

    for(auto arg = args.begin(); arg != args.end(); ++arg)
    {
        if(*arg == "--" + arg_switch)
            do_switch = true;
        else if(*arg == "--" + arg_option)
        {
            if(++arg == args.end())
            {
                // option needs a value - not found
                std::cout << arg_option_error << '\n';
                return 1;
            }
            option = *arg;
        }
    }

    std::cout << arg_switch << ": " << (do_switch ? "on":"off") << '\n';
    std::cout << arg_option << ": " << option << '\n';

    return 0;
}
./program --开关 --选项 wibble
开关: on
选项: wibble
3)否