Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.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++_Arrays - Fatal编程技术网

C++ 如何构造字符数组

C++ 如何构造字符数组,c++,arrays,C++,Arrays,我有下面的代码试图枚举 字符串 #include <string> #include <iostream> using namespace std; string base = "000"; char values[] = {'0', '1', '2', '3' }; // Error Here for (int i = 0; i < base.length(); ++i) { for (int j = 0; j < countof(values)

我有下面的代码试图枚举 字符串

#include <string>
#include <iostream>

using namespace std;

string base = "000";
char values[] = {'0', '1', '2', '3' }; // Error Here

for (int i = 0; i < base.length(); ++i)
{
   for (int j = 0; j < countof(values); ++j)
   {
      if (base[i] != values[j])
      {
          string copy = base;
          copy[i] = values[j];
          cout << copy << endl;

          for (int k = i+1; k < base.length(); ++k)
          {
              for (int l = 0; l < countof(values); ++l)
              {
                   if (copy[k] != values[l])
                   {
                       string copy2 = copy;
                       copy[k] = values[l];
                       cout << copy2 << endl;
                   }
              }
          }
      }
   }
}
#包括
#包括
使用名称空间std;
字符串base=“000”;
字符值[]={0',1',2',3'};//这里出错
对于(int i=0;icoutfor
循环中的
错误实际上在下面一行:您的代码需要包含在某种函数中,很可能是
int main(void)

错误实际上在以下行中,在
for
循环中:您的代码需要包含在某种函数中,很可能
int main(void)
您缺少一个main

尝试:

#包括
#包括
使用名称空间std;
字符串base=“000”;
字符值[]={0',1',2',3'};//此处出错
int main()//已添加
{//添加
对于(int i=0;i难道你错过了一条主线吗

尝试:

#包括
#包括
使用名称空间std;
字符串base=“000”;
字符值[]={0',1',2',3'};//此处出错
int main()//已添加
{//添加
对于(int i=0;i我马上就看到了两个主要问题

1) 您没有main()和它的返回代码,这是理所当然的

2) countof()不存在,您可能正在查找sizeof()

#包括
#包括
#定义countof(数组)(sizeof(数组)/sizeof(数组[0]))
使用名称空间std;
int main(int argc,char*argv[]){
字符串base=“000”;
字符值[]={0',1',2',3'};//此处出错
对于(int i=0;i我马上就看到了两个主要问题

1) 您没有main()和它的返回代码,这是理所当然的

2) countof()不存在,您可能正在查找sizeof()

#包括
#包括
#定义countof(数组)(sizeof(数组)/sizeof(数组[0]))
使用名称空间std;
int main(int argc,char*argv[]){
字符串base=“000”;
字符值[]={0',1',2',3'};//此处出错
对于(int i=0;i我不想太苛刻,但这就是为什么首先学习动态语言是错误的。程序的起点必须是例程,无论是显式的(如C)还是隐式的(如Python)。我不想太苛刻,但这就是为什么先学习动态语言是错误的。程序的起点必须是一个例程,无论是显式的(如C)还是隐式的(如Python)。也许他使用的是:sizeof也会给出错误的答案。您可能会想要“sizeof(values)/sizeof(values[0])最低限度。啊,我甚至没有注意到他在检查数组的大小,我会添加到宏中。也许他使用的是:sizeof也会给出错误的答案。你会希望最低限度地使用“sizeof(values)/sizeof(values[0])。啊,我甚至没有注意到他在检查数组的大小,我会添加到宏中。
test.cc:9: error: expected unqualified-id before 'for'
test.cc:9: error: expected constructor, destructor, or type conversion before '<' token
test.cc:9: error: expected unqualified-id before '++' token
#include <string>
#include <iostream>

using namespace std;

string base = "000";
char values[] = {'0', '1', '2', '3' }; // Error Here

int main() // Added
{ // Added

   for (int i = 0; i < base.length(); ++i)
   {
      for (int j = 0; j < countof(values); ++j)
      {
         if (base[i] != values[j])
         {
             string copy = base;
             copy[i] = values[j];
             cout << copy << endl;

             for (int k = i+1; k < base.length(); ++k)
             {
                 for (int l = 0; l < countof(values); ++l)
                 {
                      if (copy[k] != values[l])
                      {
                          string copy2 = copy;
                          copy[k] = values[l];
                          cout << copy2 << endl;
                      }
                 }
             }
         }
      }
   }

   return 0; // Added
} // Added
#include <string>
#include <iostream>
#define countof( array ) ( sizeof( array )/sizeof( array[0] ) )

using namespace std;

int main(int argc, char *argv[]) {

string base = "000";
char values[] = {'0', '1', '2', '3' }; // Error Here

    for (int i = 0; i < base.length(); ++i)
    {
       for (int j = 0; j < countof(values); ++j)
       {
          if (base[i] != values[j])
          {
              string copy = base;
              copy[i] = values[j];
              cout << copy << endl;

              for (int k = i+1; k < base.length(); ++k)
              {
                  for (int l = 0; l < countof(values); ++l)
                  {
                       if (copy[k] != values[l])
                       {
                           string copy2 = copy;
                           copy[k] = values[l];
                           cout << copy2 << endl;
                       }
                  }
              }
          }
       }
    } 
return 0;
}