substring或将数组打印到C++中的num位置

Ali*_*Ali 1 c++ cout substr

在PHP中我记得我可以做类似的事情

substr(string,start,length)

现在我宣布

int array[20];

如何在不使用for循环的情况下仅打印部分内容

例如.

cout << array[1 to 5] << "Here is the breaking point" << array[15 to 20] << endl;

像这样的东西

我还记得是否printf会有类似^5或类似于说到5的东西

das*_*ght 6

您可以使用ostream_iteratorcopy(链接到ideone)的组合:

#include <iostream>
#include <iterator>
#include <algorithm>

using namespace std;

int main() {
    int array[] = {1,2,3,4,5,6,7,8,9,10,11,12,13};
    ostream_iterator<int> out_it (cout," ");
    copy ( array+3, array+6, out_it );
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

array+3语法可能看起来不寻常的:这是一个指针表达式等价于&array[3],其产生的指针.由于您可以传递一对数组指针,其中C标准库需要一对迭代器,因此会产生预期的结果.