MSVC调试器告诉我,我的向量比指定的位置多

时间:2015-04-02 20:49:12

标签: c++ visual-c++ msvc12

我已宣布以下内容:

const int NUMBER_OF_DIGITS = 16;
std::vector<int> digits(NUMBER_OF_DIGITS);

但是,当我打开MSVC调试器时,它显示以下内容:

enter image description here

这就是我向向量添加值的方法:

for (int i = 0; i < NUMBER_OF_DIGITS; ++i) {
        scanf("%d", &digits[i]);
    }

这是正常的吗?我可以忽略这个吗?或者这是一个问题?

完整代码(该程序尚未准备好):

#include <iostream>
#include <vector>
#include "stdio.h"

const int NUMBER_OF_DIGITS = 16;

int checksum, tmp1, tmp2, tmp3, sum = 0;

std::vector<int> digits(NUMBER_OF_DIGITS);
std::vector<int> new_digits(NUMBER_OF_DIGITS);

int luhn_checksum(std::vector<int> cardnumber[NUMBER_OF_DIGITS]) {
    //step 1: duouble every second number
    for (int i = 1; i < NUMBER_OF_DIGITS; i += 2) {
        new_digits[i] = digits[i] * 2;
        if (new_digits[i] > 9) {
            //if the product is larger than 9 we will add the two numbers together
            //example: 9 * 2 = 18 so we will add 1 + 8 to get 9
            tmp1 += new_digits[i] % 10;
            new_digits[i] /= 10;
            tmp1 = 0;
        }
    }

    //step 2: sum all the values
    for (int i = 0; i < NUMBER_OF_DIGITS; ++i) {
        checksum += new_digits[i];
    }

    return checksum;
}

void get_card_numbers(void) {
    for (int i = 0; i < NUMBER_OF_DIGITS; ++i) {
        scanf("%d", &digits[i]);
    }
}

void initialize(void) {
    for (int i = 0; i < NUMBER_OF_DIGITS; ++i) {
        digits.push_back(0);
        new_digits.push_back(0);
    }
}


int main() {
    initialize();

    get_card_numbers();

    printf("checksum is: %d\n", luhn_checksum(&digits));


    std::cout << digits.size() << std::endl;


    int x; scanf("%d", &x);
    return 0;
}

2 个答案:

答案 0 :(得分:1)

您为digits使用的构造函数是通过指定计数来设置大小。因此,在致电push_back之后,您已向向量添加了另外16个。使用不设置计数的其他构造函数。

int _tmain(int argc, _TCHAR* argv[])
{
    const int NUMBER_OF_DIGITS = 16;

    std::vector<int> digits(NUMBER_OF_DIGITS);
    //std::vector<int> digits;

    int digitsLen = digits.size();
    // Here is 16

    for (int i = 0; i < NUMBER_OF_DIGITS; ++i) {
        digits.push_back(0);
    }

    int digitsLen2 = digits.size();
    // Here is 32

    return 0;
}

答案 1 :(得分:0)

稍微清理一下你的代码:

#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

static const size_t NUMBER_OF_DIGITS = 16;

class cards {
public:
   cards();
   void read();
   int luhnChecksum() const;
private:
   vector<int> digits;
};

cards::cards() : digits(NUMBER_OF_DIGITS, 0) 
{
}

void cards::read() {
   for_each(digits.begin(), digits.end(), [](int& i) { cin >> i; });
}

int cards::luhnChecksum() const {
   vector<int> newdigits(digits.begin(), digits.end());
   for (size_t i=1; i<NUMBER_OF_DIGITS; i += 2) {
      newdigits[i] = digits[i] * 2;
      if (newdigits[i] > 9) {
     int tmp1 = newdigits[i] % 10;
     newdigits[i] /= 10;
     newdigits[i] += tmp1;
      }
   }

   return accumulate(newdigits.begin(), newdigits.end(), 0);
}

int main() {
   cards c;
   c.read();
   cout << "checksum = " << c.luhnChecksum() << endl;
   return 0;
}