Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/jpa/2.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+后返回向量顶部+;_C++_Vector - Fatal编程技术网

C++ 到达c+后返回向量顶部+;

C++ 到达c+后返回向量顶部+;,c++,vector,C++,Vector,我现在有一个迭代器,它通过一个向量随机递增,我希望在到达终点后能够返回到向量的顶部。我在随机生成器中放置了一个种子,所以我有相同的序列。显然,此时向量超出了范围 int main() { vector<int> vectorTest = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20}; vector <int>::iterator it; for (it

我现在有一个迭代器,它通过一个向量随机递增,我希望在到达终点后能够返回到向量的顶部。我在随机生成器中放置了一个种子,所以我有相同的序列。显然,此时向量超出了范围

int main() 
{
vector<int> vectorTest = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 
10, 11, 12, 13, 14, 15, 16,
    17, 18, 19, 20};  

vector <int>::iterator it;

for (it = vectorTest.begin(); it != vectorTest.end(); it = it + rand() % 6)
{ 
    cout << *it << endl;    
}
}
intmain()
{
向量向量测试={1,2,3,4,5,6,7,8,9,
10, 11, 12, 13, 14, 15, 16,
17, 18, 19, 20};  
向量::迭代器;
for(it=vectorTest.begin();it!=vectorTest.end();it=it+rand()%6)
{ 

cout这不是迭代器的用例。我建议使用以向量长度模为界的通用索引

int j = 0;
for (int i = 0; j < 20 ; i = (i + rand() % 6) % vectorTest.size(), j++) {
    cout << vectorTest[i] << endl;
}
intj=0;
对于(inti=0;j<20;i=(i+rand()%6)%vectorTest.size(),j++){

cout这不是迭代器的用例。我建议使用以向量长度模为界的通用索引

int j = 0;
for (int i = 0; j < 20 ; i = (i + rand() % 6) % vectorTest.size(), j++) {
    cout << vectorTest[i] << endl;
}
intj=0;
对于(inti=0;j<20;i=(i+rand()%6)%vectorTest.size(),j++){
cout可以用来无限期地重复向量元素

int main() 
{
    std::vector<int> vectorTest = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20};  

    auto repeating = vectorTest | ranges::view::cycle;

    for (auto it = repeating.begin(); /* how to end? */; it = it + rand() % 6)
    { 
        std::cout << *it << std::endl;    
    }
}
intmain()
{
向量向量测试={1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20};
自动重复=向量测试|范围::视图::循环;
对于(自动it=repeating.begin();/*如何结束?*/;it=it+rand()%6)
{ 
std::cout可以用来无限期地重复向量元素

int main() 
{
    std::vector<int> vectorTest = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20};  

    auto repeating = vectorTest | ranges::view::cycle;

    for (auto it = repeating.begin(); /* how to end? */; it = it + rand() % 6)
    { 
        std::cout << *it << std::endl;    
    }
}
intmain()
{
向量向量测试={1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20};
自动重复=向量测试|范围::视图::循环;
对于(自动it=repeating.begin();/*如何结束?*/;it=it+rand()%6)
{ 

std::cout如果要包装,可以执行类似于
it=(it+rand()%6)%vectorTest.size
的操作。如果包装迭代器,则永远无法到达
vectorTest.end()
。循环何时停止?仅使用索引如何?迭代器不是用于此目的。@user463035818我希望循环在20后停止。是否可能需要数组的随机排列?或者是否确实要选择20个随机元素,包括重复?对于随机排列,存在现成的解决方案n在
中,我认为如果您想要包装,您可以执行类似
it=(it+rand()%6)%vectorTest.size
的操作。如果包装迭代器,您将永远无法到达
vectorTest.end()
。循环何时停止?仅使用索引如何?迭代器不是用于此目的。@user463035818我希望循环在20后停止。是否可能需要数组的随机排列?或者是否确实要选择20个随机元素,包括重复?对于随机排列,存在现成的解决方案n在
中,我认为迭代器也很好,同样:
for(auto it=v.begin();j++<20;std::advance(it,std::min(rand()%6,std::distance(it,v.end()))
迭代器也很好,同样:
for(auto it=v.begin();j++<20;std::advance(it,std::min(rand()%6,std::distance(it,v.end()))