小编Din*_*har的帖子

为什么 for 循环没有停止?

for我的程序中有一个没有停止的循环:

vector<int> v;
int k = 1;
v.push_back(1);

for(int i=v.size()-1; i>=(v.size()-k); i--){
    cout << i << " " << v.size() - k << endl;
}
Run Code Online (Sandbox Code Playgroud)

当我执行上述程序时,它一直在for循环内运行。for循环的输出如下:

0 0
-1 0
-2 0
-3 0
-4 0
.
.
.
Run Code Online (Sandbox Code Playgroud)

根据输出,值i递减,值为v.size()-k0。所以,i >= v.size()-k为假,应该for在第一轮后停止循环执行,但for循环不会停止执行。

有人可以帮我理解这个问题吗?

c++ for-loop vector

1
推荐指数
1
解决办法
87
查看次数

Visual Studio - 奇怪的字符输出

我在visual studio 2013中运行这个程序,但它正在打印奇怪的字符.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

typedef struct{
    char first[11];
    char second[11];
}INFO;

char * str1;
char * str2;
int num;

void bar_function(INFO info)
{
    str1 = info.first;
    str2 = info.second;
    num = 100;
    printf("%s %s\n", str1, str2);
}

void print()
{
    printf("%s %s\n", str1, str2);
    printf("%d\n", num);
}

int main(void)
{
    INFO info;
    strcpy(info.first, "Hello ");
    strcpy(info.second, "World!");

    bar_function(info);
    print();
    system("pause");
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

bar_function函数中,我将值赋给全局变量并打印字符串.它打印正确的输出.但是当我在打印功能中打印相同的字符串时,它在visual studio 2013的输出中打印出奇怪的字符.同时num变量在打印 …

c++ visual-studio visual-studio-2013

0
推荐指数
1
解决办法
257
查看次数