英文:
C++ ranged vs manual for loop
问题
I am new to C++ and for practice I have been solving some problems on projecteuler.net. One of the questions involves analyzing a 1000 digit number, so I wrote a program that can read in the number and stores it in a vector. In order to test my code, I have printed out the vector to the console.
I am confused because if I use a manually specified indexed for
loop, it prints out the correct values, but when I use ranged for
with the auto&
keyword it gets confused and prints out wrong values.
Here is my code:
#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];
}
This version prints out the correct 1000 digit number, but if I use
for(auto& i : numbers)
cout << numbers[i];
The digits are wrong.
The correct number is
731671765313306249192251196744265747423553491949349698352031277450632623957831801698480186947885184385861560789112949495459501737958
331952853208805511125406987471585238630507156932909632952274430435576689664895044524452316173185640309871112172238311362229893423380
308135336276614282806444486645238749303589072962904915604407723907138105158593079608667017242712188399879790879227492190169972088809
377665727333001053367881220235421809751254540594752243525849077116705560136048395864467063244157221553975369781797784617406495514929
086256932197846862248283972241375657056057490261407972968652414535100474821663704844031998900088952434506585412275886668811642717147
992444292823086346567481391912316282458617866458359124566529476545682848912883142607690042242190226710556263211111093705442175069416
589604080719840385096245544436298123098787992724428490918884580156166097919133875499200524063689912560717606058861164671094050775410
0225698315520005593572972571636269561882670428252483600823257530420752963450
And the "shifted" one is
564354538646633990409984403599938595996886904090690302689364955983369396085264234302923423095228429628234833520449090908980834565082
664089286932238844498933025954828962363835483069030369089959963968853320339208399899989643456428393630254449459962644639990206996623
632468663953349929233999923398962590636820359039039048339935596035462438482806350332335345999549422600250503250995909403430059322230
655338595666334386635224993968994230584989893809589996898290355443538833463392608239935336999485994886058630524505529345933908849090
323983069405293239992926059994658385383385903934935059032389949868433959294336539299364002033322089969833828949958223332244399545495
009999909296323693835924604049643929982345233982680499833890953898329292049226499335303399999403993543883936944444306538999458330943
820339323540293628303998899963902496302525009599992903042229823483433305040466258900933899336320049833545333382234439354309383558943
3998302648893338806859059854363930834229353992989926333296985863993589036983
Could anyone explain to me what is going on?
英文:
I am new to C++ and for practice I have been solving some problems on projecteuler.net. One of the questions involves analyzing a 1000 digit number, so I wrote a program that can read in the number and stores it in a vector. In order to test my code, i have printed out the vector to the console.
I am confused because if I use a manually specified indexed for
loop, it prints out the correct values, but when I use ranged for
with the auto&
keyword it gets confused and prints out wrong values.
Here is my code:
#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];
}
This version prints out the correct 1000 digit number, but if i use
for(auto& i : numbers)
cout << numbers[i];
The digits are wrong.
The correct number is
731671765313306249192251196744265747423553491949349698352031277450632623957831801698480186947885184385861560789112949495459501737958
331952853208805511125406987471585238630507156932909632952274430435576689664895044524452316173185640309871112172238311362229893423380
308135336276614282806444486645238749303589072962904915604407723907138105158593079608667017242712188399879790879227492190169972088809
377665727333001053367881220235421809751254540594752243525849077116705560136048395864467063244157221553975369781797784617406495514929
086256932197846862248283972241375657056057490261407972968652414535100474821663704844031998900088952434506585412275886668811642717147
992444292823086346567481391912316282458617866458359124566529476545682848912883142607690042242190226710556263211111093705442175069416
589604080719840385096245544436298123098787992724428490918884580156166097919133875499200524063689912560717606058861164671094050775410
0225698315520005593572972571636269561882670428252483600823257530420752963450
And the "shifted" one is
564354538646633990409984403599938595996886904090690302689364955983369396085264234302923423095228429628234833520449090908980834565082
664089286932238844498933025954828962363835483069030369089959963968853320339208399899989643456428393630254449459962644639990206996623
632468663953349929233999923398962590636820359039039048339935596035462438482806350332335345999549422600250503250995909403430059322230
655338595666334386635224993968994230584989893809589996898290355443538833463392608239935336999485994886058630524505529345933908849090
323983069405293239992926059994658385383385903934935059032389949868433959294336539299364002033322089969833828949958223332244399545495
009999909296323693835924604049643929982345233982680499833890953898329292049226499335303399999403993543883936944444306538999458330943
820339323540293628303998899963902496302525009599992903042229823483433305040466258900933899336320049833545333382234439354309383558943
3998302648893338806859059854363930834229353992989926333296985863993589036983
Could anyone explain to me what is going on?
答案1
得分: 6
auto i
,i
是 数字,而不是在 numbers
数组中的索引。
所以你只需要 cout << i;
。
英文:
With auto& i
, i
is the number, not the index to the number in the numbers
array.
So you simply need cout << i;
答案2
得分: 5
auto& i : numbers makes it so that i takes the values inside the vector. Not the indexes!
英文:
for(auto& i : numbers)
cout << i;
Is what you are looking for. auto& i : numbers makes it so that i takes the values inside the vector. Not the indexes!
答案3
得分: 3
你从经典的索引计数循环转向了针对元素的for-range循环:
计数循环:
for (int i = 0; i < numbers.size(); i++)
cout << numbers[i];
for-range循环:
for (auto& i : numbers)
cout << i;
你的奇怪的混合体从一个循环中取了头部,从另一个循环中取了身体:
for (auto& i : numbers)
cout << numbers[i];
将后者翻译成经典的计数循环:
for (int i = 0; i < numbers.size(); i++)
cout << numbers[numbers[i]];
现在应该很明显为什么输出不同了。
英文:
You went half-way from a classic counting loop for indices to a for-range loop over elements:
Counting loop:
for (int i = 0; i < numbers.size(); i++)
cout << numbers[i];
For-range loop:
for (auto& i : numbers)
cout << i;
Your weird chimaera takes the head from one and the body from the other:
for (auto& i : numbers)
cout << numbers[i];
Translating the latter to a classic counting loop:
for (int i = 0; i < numbers.size(); i++)
cout << numbers[numbers[i]];
Now it should be obvious why the output is different.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论