Const char到Array C ++

时间:2015-01-15 15:02:17

标签: c++ arrays char

我遇到了生成Word的所有子序列的函数的问题。问题是:

cout << *(s + y) ;

这行打印是一个字母,我想添加这个&#34; char&#34;到一个数组

像:

char str[100];
str+=(cout << *(s + y) );

我知道这是错的但有办法吗?要通过char将char添加到数组中? (这种类型*(s + y)也很厌烦,编译器显示很多问题)

功能:

void encontra(const char *s)
{
    string str;
    while(*s)
    {
        int x=0;
        while(*(s + x))
        {
            for(int y = 0; y <= x; y++)
                cout << *(s + y) ;
            cout << "\n";
            x++;
        }
        s++;
    }
}

1 个答案:

答案 0 :(得分:1)

表达式有两个问题

str+=(cout << *(s + y) )

第一个是输出运算符<<返回对流的引用。

第二个问题是你无法附加到这样的数组。

而是使用std::string,然后您可以轻松地将字符附加到字符串:

std::string str;
std::cout << s[y];
str += s[y];
相关问题