vectorを関数へ渡した結果が違う

フォーラム(掲示板)ルール
フォーラム(掲示板)ルールはこちら  ※コードを貼り付ける場合は [code][/code] で囲って下さい。詳しくはこちら
kanse
記事: 6
登録日時: 10年前

vectorを関数へ渡した結果が違う

#1

投稿記事 by kanse » 10年前

構造体をvector()にしてそれを関数に渡して、計算させた結果を関数の外で使用したくて、サンプルとして以下のように書いてみました。
ところが関数内では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;
	}
}

アバター
みけCAT
記事: 6734
登録日時: 14年前
住所: 千葉県
連絡を取る:

Re: vectorを関数へ渡した結果が違う

#2

投稿記事 by みけCAT » 10年前

kanse さんが書きました:構造体をvector()にしてそれを関数に渡して、計算させた結果を関数の外で使用したくて、サンプルとして以下のように書いてみました。
ところが関数内ではt[1].time = 901と出力されるのですが、外では全く違う値が出力されました。
C++では関数の引数はコピーが渡されるので、関数内で(参照ではない)引数の値を変えても、呼び出し元には影響を与えません。
kanse さんが書きました:どのようにすれば関数の外まで保存できるのでしょうか?
参照を使うといいでしょう。

コード:

#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;
	}
}
複雑な問題?マシンの性能を上げてOpenMPで殴ればいい!(死亡フラグ)

kanse
記事: 6
登録日時: 10年前

Re: vectorを関数へ渡した結果が違う

#3

投稿記事 by kanse » 10年前

解決しました。
ありがとうございました。

閉鎖

“C言語何でも質問掲示板” へ戻る