首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >TCHAR二维阵列

TCHAR二维阵列
EN

Stack Overflow用户
提问于 2018-05-18 21:36:09
回答 1查看 465关注 0票数 0

我试图创建两个矩阵:一个为char*,一个为THAR*。但是对于TCHAR*矩阵,而不是字符串,我得到了某种地址。怎么了?

代码:

代码语言:javascript
复制
#include <tchar.h>
#include <iostream>

using namespace std;

int main(int argc, _TCHAR* argv[])
{
    //char
    const char* items1[2][2] = {
        {"one", "two"},
        {"three", "four"},
    };

    for (size_t i = 0; i < 2; ++i)
    {
        cout << items1[i][0] << "," << items1[i][1] <<endl;
    }

    /*
    Correct output:
        one,two
        three,four
    */

    //TCHAR attempt
    const TCHAR* items2[2][2] = {
        {_T("one"), _T("two")},
        {_T("three"), _T("four")},
    };

    for (size_t i = 0; i < 2; ++i)
    {
        cout << items2[i][0] << "," << items2[i][1] <<endl;
    }

    /*
    Incorrect output:
        0046AB14,0046AB1C
        0046AB50,0046D8B0
    */

    return 0;
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-05-18 22:13:04

要解决这个问题,我们需要对Unicode字符串使用wcout。使用string,我们可以创建灵活的tcout

代码语言:javascript
复制
#include <tchar.h>
#include <iostream>

using namespace std;

#ifdef UNICODE
    wostream& tcout = wcout;
#else
    ostream& tcout = cout;
#endif // UNICODE

int main(int argc, _TCHAR* argv[])
{
    //char
    const char* items1[2][2] = {
        {"one", "two"},
        {"three", "four"},
    };

    for (size_t i = 0; i < 2; ++i)
    {
        tcout << items1[i][0] << "," << items1[i][1] <<endl;
    }

    /*
    Correct output:
        one,two
        three,four
    */

    //TCHAR attempt
    const TCHAR* items2[2][2] = {
        {_T("one"), _T("two")},
        {_T("three"), _T("four")},
    };

    for (size_t i = 0; i < 2; ++i)
    {
        tcout << items2[i][0] << "," << items2[i][1] <<endl;
    }

    /*
    Correct output:
        one,two
        three,four
    */

    return 0;
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50419569

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档