ところが関数内ではt[1].time = 901と出力されるのですが、外では全く違う値が出力されました。
そこで初期値のままなのかな?と思い、関数の行の前に表示させてみたら、同じ値でした。
どのようにすれば関数の外まで保存できるのでしょうか?
#include <iostream>
//名前空間指定することでstd::を記載する必要がなくなる
#include <vector>
#include <string>
#include <random>
#include <algorithm>
using namespace std;
typedef struct {
int day;int time;
int buy_ita[8]; int sell_ita[8];
int buy_price[8]; int sell_price[8];
int ryou; int price;
int limit_delta_buy[8], limit_delta_price_buy[8];
int limit_delta_sell[8], limit_delta_price_sell[8];
int delta_price;
int trading_volume;
}Ita;
void keisan(vector<Ita> t, int max_gyou);
int main()
{
//vector()構造体
vector<Ita> t;
int gyou = 10;
//test
t.push_back(Ita());
cout << t[1].time << ":" << t[1].sell_ita[1] << endl;
keisan(t, gyou);
cout << t[1].time << ":"<<t[1].sell_ita[1] << endl;
system("pause");
return 0;
}
void keisan(vector<Ita> v, int max_gyou) {
for (int i = 0; i < max_gyou; i++) {
v.push_back(Ita()); // add 要素にアクセスする前に、push_backやresize、コンストラクタなどで領域(要素)を確保しないといけない。
v[i].time = 900 + i;
for (int j = 0; j < 8; j++) {
v[i].buy_ita[j] = 1000 * j + i;
v[i].sell_ita[j] = 1000 * j + 2;
}
cout << v[i].time << "," << v[i].buy_ita[1] << endl;
}
}