Skip to content

Instantly share code, notes, and snippets.

@buyoh
Last active January 9, 2017 01:41
Show Gist options
  • Save buyoh/ffd2f73da682991d15cbbd6e66a7c365 to your computer and use it in GitHub Desktop.
Save buyoh/ffd2f73da682991d15cbbd6e66a7c365 to your computer and use it in GitHub Desktop.
本来書きたかったコードと間違ったコード.コンパイルも通り,セグメンテーション違反も無い.
#include<bits/stdc++.h>
using namespace std;
struct Hoge{
int a,b,c;
Hoge(int a=0,int b=0,int c=0):a(a),b(b),c(c){}
};
int main(){
vector<int> mat;
vector<Hoge> hooge;
mat.push_back(hooge.size());
hooge.emplace_back(1,2,3);
mat.push_back(hooge.size());
hooge.emplace_back(100,200,300);
// ---------------------
// 本来書きたかったコード
for (int i : mat){
const Hoge& h = hooge[i];
printf("%d %d %d\n",h.a, h.b, h.c);
}
printf("-----\n");
// ---------------------
// 間違って書いたコード
for (const Hoge& h : mat){ // matはvector<int>型.
printf("%d %d %d\n",h.a, h.b, h.c);
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment