我正在尝试从第二个索引到最后一个索引对我的 C++ 数组进行切片

I'm trying to slice my c++ array from the second to the last index

我正在尝试在 C++ 中模拟 echo 命令。 我正在尝试从条目值中切出程序名称并推送 休息到命令行。但是我遇到了奇怪的错误。 这是我的代码:

#include <iostream>
using namespace std;

int main(int argc, char const *argv[]) {
  if(argv[1] == "echo"){
    cout << args[2:];
  }
  return 0;
}

但我收到错误:

    cmd.cpp: In function 'int main(int, const char**)':
    cmd.cpp:6:13: error: 'args' was not declared in this scope
         cout << args[:];
                 ^~~~
    cmd.cpp:6:13: note: suggested alternative: 'argc'
         cout << args[:];
                 ^~~~
                 argc
    cmd.cpp:6:18: error: expected primary-expression before ':' token
         cout << args[:];
                      ^
    cmd.cpp:6:18: error: expected ']' before ':' token
         cout << args[:];
                      ^
                      ]

我正在尝试将 {1234545, "hello", ", world!"} 变成 "hello, world!" 基本上我想做的是摆脱数组 [0] 并加入列表的其余部分一起。

编辑:感谢@chipster 给出了很好的答案!

小问题(我的意思是,我想这是一个大问题,因为它是导致编译器错误的问题,但是一旦修复它,您很快就会遇到另一个错误,所以... ): args 不存在。您实际上想要 argv


语法 arr[i:j] 是 Python 语法,不是 C++。

要在 C++ 中执行等效操作,请改为执行以下操作:

for(int i=2;i<argc;i++) {
    std::cout << argv[i] << "\n"; // "\n" is just to make things look nicer.
        // "\n" could be any separator
}