C++ ranged 与手动 for 循环

nub*_*lot 3 c++ for-loop ranged-loops

我是 C++ 新手,为了练习,我一直在解决projecteuler.net上的一些问题。其中一个问题涉及分析一个 1000 位数字,因此我编写了一个程序,可以读取该数字并将其存储在一个向量中。为了测试我的代码,我已将向量打印到控制台。我很困惑,因为如果我使用手动指定的索引for循环,它会打印出正确的值,但是当我将 rangedforauto&关键字一起使用时,它会感到困惑并打印出错误的值。

这是我的代码:

#include <iostream>
#include <fstream>
#include <vector>
using namespace std;

vector<int8_t> reader(string datei)
{
    ifstream file;
    char inputChar;
    vector<int8_t> data;
    file.open(datei);
    while(file >> inputChar) 
        data.push_back(inputChar);
    file.close();

    return data;
}

int main()
{
    string a;
    cout << "Select file to open:" << endl;
    cin >> a;
    vector<int8_t> numbers = reader(a);
    for(int i = 0; i < numbers.size(); i++)
       cout << numbers[i];
    for(int i = 0; i < index.size(); i++)
       cout << index[i];
}
Run Code Online (Sandbox Code Playgroud)

此版本打印出正确的 1000 位数字,但如果我使用

for(auto& i : numbers)
           cout << numbers[i];
Run Code Online (Sandbox Code Playgroud)

数字是错误的。正确的数字是

731671765313306249192251196744265747423553491949349698352031277450632623957831801698480186947885184385861560789112949495459501737958
331952853208805511125406987471585238630507156932909632952274430435576689664895044524452316173185640309871112172238311362229893423380
308135336276614282806444486645238749303589072962904915604407723907138105158593079608667017242712188399879790879227492190169972088809
377665727333001053367881220235421809751254540594752243525849077116705560136048395864467063244157221553975369781797784617406495514929
086256932197846862248283972241375657056057490261407972968652414535100474821663704844031998900088952434506585412275886668811642717147
992444292823086346567481391912316282458617866458359124566529476545682848912883142607690042242190226710556263211111093705442175069416
589604080719840385096245544436298123098787992724428490918884580156166097919133875499200524063689912560717606058861164671094050775410
0225698315520005593572972571636269561882670428252483600823257530420752963450
Run Code Online (Sandbox Code Playgroud)

而“移位”的是

564354538646633990409984403599938595996886904090690302689364955983369396085264234302923423095228429628234833520449090908980834565082
664089286932238844498933025954828962363835483069030369089959963968853320339208399899989643456428393630254449459962644639990206996623
632468663953349929233999923398962590636820359039039048339935596035462438482806350332335345999549422600250503250995909403430059322230
655338595666334386635224993968994230584989893809589996898290355443538833463392608239935336999485994886058630524505529345933908849090
323983069405293239992926059994658385383385903934935059032389949868433959294336539299364002033322089969833828949958223332244399545495
009999909296323693835924604049643929982345233982680499833890953898329292049226499335303399999403993543883936944444306538999458330943
820339323540293628303998899963902496302525009599992903042229823483433305040466258900933899336320049833545333382234439354309383558943
3998302648893338806859059854363930834229353992989926333296985863993589036983
Run Code Online (Sandbox Code Playgroud)

谁能向我解释发生了什么?

Bat*_*eba 6

其中auto& i,i 数字,而不是numbers数组中数字的索引。

所以你只需要 cout << i;


小智 5

for(auto& i : numbers)
    cout << i;
Run Code Online (Sandbox Code Playgroud)

是你要找的。auto& i : numbers 使我能够获取向量中的值。不是索引!