2016-07-16 6 views
-2

Ich habe einen Code-Block, in dem ich vier Vektoren deklariere, zwei davon initialisiere. Dann habe ich zwei for-Schleifen, wo ich Elemente zu nicht initialisierten Vektoren hinzufüge. Dann lege ich endlich alle Elemente eines Vektors einem anderen zu.Vektor-Index außerhalb des Bereichs Ausgabe

Aber während ich dies tue bekomme ich diesen Fehler - "Vektor-Index außerhalb des Bereichs". Gibt es irgendeine Operation, die ich auf Vektoren mache, die ich falsch mache?

Mein Code:

int main() 
{ 
    std::vector<int> K; 
    K.insert(K.begin(),0); 
    std::vector<int> tempK; 
    std::vector<int> S; 
    S.insert(S.begin(),0); 
    std::vector<int> tempS; 
    int n; 
    float exptd = 0; 
    float SD = 0; 
    std::cout<<"How many steps: "; 
    std::cin >> n; 
    for (int j= 1; j<=n; j++) { 
     for (int i=1; i<=K.size(); i++) { 
      if (K[i] == 0) { 
       tempK.push_back(4); tempK.push_back(6); 
       tempS.push_back(K[i]+4); tempS.push_back(K[i] +6); 
      } 
      else if (K[i] == 1) { tempK.push_back(6); tempK.push_back(8); 
       tempS.push_back(K[i]+6); tempS.push_back(K[i] +8); 
      } 
      else if (K[i] == 2) { 
       tempK.push_back(7); tempK.push_back(9); 
       tempS.push_back(K[i]+7); tempS.push_back(K[i] +9); 
      } 
      else if (K[i] == 3) { 
       tempK.push_back(4); tempK.push_back(8); 
       tempS.push_back(K[i]+4); tempS.push_back(K[i] +8); 
      } 
      else if (K[i] == 4) { 
       tempK.push_back(3); tempK.push_back(9); tempK.push_back(0); 
       tempS.push_back(K[i]+3); tempS.push_back(K[i] +9); 
       tempS.push_back(K[i] +0); 
      } 
      else if (K[i] == 6) { 
       tempK.push_back(1); tempK.push_back(7); tempK.push_back(0); 
       tempS.push_back(K[i]+1); tempS.push_back(K[i] +7); 
       tempS.push_back(K[i] +0); 
      } 
      else if (K[i] == 7) { 
       tempK.push_back(2); tempK.push_back(6); 
       tempS.push_back(K[i]+2); tempS.push_back(K[i] +6); 
      } 
      else if (K[i] == 8) { 
       tempK.push_back(1); tempK.push_back(3); 
       tempS.push_back(K[i]+1); tempS.push_back(K[i] +3); 
      } 
      else if (K[i] == 9) { 
       tempK.push_back(2); tempK.push_back(4); 
       tempS.push_back(K[i]+2); tempS.push_back(K[i] +4); 
      } 
     } 
     S = tempS; 
     tempS.clear(); 
     tempS.resize(100); 
     K = tempK; 
     tempK.clear(); 
     tempK.resize(100); 
    } 
} 
+0

Indizes gehen von '0' auf 'size-1'. Deine 'for()' Loops gehen von '1' nach' size'. Die letzte Iteration greift also außerhalb des Vektors zu. – Barmar

Antwort

0

Das ist das Problem:

for (int i=1; i<=K.size(); i++) 

Vector Indizes bei 0 beginnen, und der höchste Index ist K.size()-1. Wenn iK.size() erreicht, greift K[i] außerhalb des Vektors zu.

es sein sollte:

for (int i = 0; i < K.size(); i++) 
+0

Danke für die Antwort, aber der Fehler ist immer noch persistent .. – David

+0

In welcher Zeile ist der Fehler passiert? – Barmar

+0

Funktioniert jetzt gut, danke Bamar – David