C++ 向量推回时钟?

C++ 向量推回时钟?,c++,vector,sfml,C++,Vector,Sfml,使用Vector,我可以在for循环中以正常的方式推回,但速度非常快。我正试图编码它,但我得到了一些错误。没有计时时没有错误。我无法修复它,也无法在互联网上找到它 while (window.isOpen()) { Time time = clock.getElapsedTime(); second = time.asSeconds(); for (int i = 0; i < randx.size(); i++) { rect.setPosit

使用Vector,我可以在for循环中以正常的方式推回,但速度非常快。我正试图编码它,但我得到了一些错误。没有计时时没有错误。我无法修复它,也无法在互联网上找到它

while (window.isOpen()) {

    Time time = clock.getElapsedTime();
    second = time.asSeconds();

    for (int i = 0; i < randx.size(); i++) {
        rect.setPosition(rand() % 300, rand() % 500);
        if (second == 2) {
            rectshape.push_back(rect);
            clock.restart();
        }
    }
while(window.isOpen()){
Time=clock.getElapsedTime();
秒=time.asSeconds();
对于(int i=0;i

看起来所有周期的迭代都将在“秒”的值等于“2”之前完成

所以你可能会使用睡眠功能而不是“如果”,例如,试试“信息”

此外,如果你不想在整个程序中睡眠,请检查正确答案

UPD:我把它改成了vector的push_backs。 我想这是你想要的

#include <stdio.h>
#include <time.h>
#include <vector>
#include <iostream>
using namespace std;

const int NUM_SECONDS = 2;
int main()
{
   int count = 1;

   double time_counter = 0;

   clock_t this_time = clock();
   clock_t last_time = this_time;

   vector<int> vect;

   while (true)
   {
       this_time = clock();

       time_counter += (double)(this_time - last_time);

       last_time = this_time;

       if (time_counter > (double)(NUM_SECONDS * CLOCKS_PER_SEC))
       {
           time_counter -= (double)(NUM_SECONDS * CLOCKS_PER_SEC);
           vect.push_back(count);
           cout << count;
           count++;
       }

   }

   return 0;
}

#包括
#包括
#包括
#包括
使用名称空间std;
const int NUM_SECONDS=2;
int main()
{
整数计数=1;
双时间计数器=0;
时钟这个时间=时钟();
时钟最后时刻=这个时刻;
向量向量;
while(true)
{
这个时间=时钟();
时间计数器+=(双精度)(此时间-上次时间);
上次=这次;
如果(时间计数器>(双精度)(数秒*时钟每秒))
{
时间计数器-=(双精度)(数秒*时钟每秒);
向量推回(计数);

cout“vector subscript out range”(向量下标超出范围)可能意味着您试图访问超出范围的索引。这意味着您显示的代码(根本没有从向量访问的语句)可能不需要对此负责。拿起VisualStudio的提议,允许您调试。通常使用VisualStudio窗口右下角的调用堆栈来查找代码中的代码,您在哪里询问了<代码> vector < /代码>的无效值。确定程序为什么使用无效的索引值并修复它。最后一步是硬PAR。t、 前两个应该不费吹灰之力。好的,但是调用堆栈是空的。谢谢,我已经读到了错误。但是我不明白为什么在for循环之外的时间将它推回时会出现这个错误。我想理解这个逻辑。我想这与时钟无关。我通过e提供数字做了同样的事情正在输入条件。问题未更改。代码: