首次运行后C ++循环跳过

时间:2015-08-27 13:23:45

标签: c++

我有一段代码,在我的生活中,我无法弄清楚为什么循环会在第一次运行后跳过。

if (*itr2 != 0)
     {
         int s = *itr2;
         //cout << s << endl;
         k = k + s;
         cout << "X: " << x << " K: " << k << " S: " << s << endl;
             for (int i = x; i < s; i++)
             {   
                 cout << "test" << endl;
                 string value = allSub[i];
                 cout << value << endl;

                 vector<string>::iterator it2;
                 vector<string>::iterator it3;
                 it2 = find(subCode.begin(), subCode.end(), value);
                 int pos = distance (subCode.begin(), it2);
                 adMatrix [pos][pos] ++;
                    for (int v = 0; v < i; v++)
                    {
                        string value2 = allSub[v];
                        cout << "does it run: " << value2 << endl;
                        it3 = find(subCode.begin(), subCode.end(), value2);
                        int pos2 = distance (subCode.begin(), it3);                           
                        adMatrix [pos][pos2] ++;
                        adMatrix [pos2][pos] ++;
                    }  
                 x = i;
             }
        x++;
     }

假设x和k最初初始化为0。输出如下:

 X: 0 K: 4 S: 4
 test
 CSCI203
 test
 CSCI235
 does it run: CSCI203
 test
 CSCI222
 does it run: CSCI203
 does it run: CSCI235
 test
 CSCI205
 does it run: CSCI203
 does it run: CSCI235
 does it run: CSCI222
 X: 4 K: 7 S: 3
 X: 5 K: 11 S: 4
 X: 6 K: 13 S: 2
 X: 7 K: 17 S: 4

为什么x = 4之后的值不通过for循环?

1 个答案:

答案 0 :(得分:0)

for(int i = x; i < s; i++) {

检查你的后续迭代:s小于x,所以我的初始值高于s,循环永远不会执行。