ページ 11

vector配列を使って単語の長さ順にソートする

Posted: 2018年6月02日(土) 15:04
by わりばし
学校の宿題でvector配列を用いて単語を長さ順にソートするということをしているのですが,
反復子を使うのかな,と思い,
std::vector<char>::iterator p= vector.begin();
と書くと「vector.begin()」の「vector」に赤波線が引かれてエラーが出ます.
コード全文は以下のような感じです.どうすればいいでしょうか

コード:

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


bool precede();

//メイン関数
void main() {

	ifstream inFile;      //入力ファイル
	string word;          //単語
	vector<string> vector;//vector配列
	std::vector<char>::iterator p= vector.begin();
	//入力ファイルを開く
	inFile.open("test.txt");

	// 入力ファイルが開けなければ終了する
	if (!inFile) {
		cout << "入力ファイルを開けません" << endl;
		return;
	}

	//ファイルから単語を読み込む
	inFile >> word;

	//ファイルの末尾でなければ、以下の処理を繰り返す
	while (!inFile.eof()) {

		//単語を格納する
		vector.push_back(word);

		//新しい単語を読み込む
		inFile >> word;
	}
	precede();
	sort(vector.begin(), vector.end(), precede);
	//単語を出力する
	for (unsigned i = 0; i<vector.size(); i++) {
		cout << vector[i] << endl;
	}
}

bool precede() {
	int str1, str2;

	if (sizeof(str1) < sizeof(str2)) {
		return true;
	}
	else {
		return false;
	}
}

Re: vector配列を使って単語の長さ順にソートする

Posted: 2018年6月02日(土) 18:13
by 結城紬
"vector" はクラス名としてすでに使われているので、変数名として使うことはできません。
変数名を別の名前に変えてください。

Re: vector配列を使って単語の長さ順にソートする

Posted: 2018年6月02日(土) 21:10
by Bull
わりばし さんが書きました:
7年前
反復子を使うのかな,と思い,
std::vector<char>::iterator p= vector.begin();
と書くと「vector.begin()」の「vector」に赤波線が引かれてエラーが出ます.
イテレーターの書き方が間違えていますね。

コード:

std::vector<std::string>::iterator p = vector.begin();
とすべきでしょう。
または、C++11 以降なら

コード:

auto p = vector.begin();
でもいいですが、p は使われていないようです。

コード:

bool precede();
も変ですね。
文字列の長さを比較するのに、肝心の文字列が渡されていません。
次のようにすればいいと思います。

コード:

bool precede(const std::string &str1, const std::string &str2)
{
	if (str1.length() < str2.length())
		return true;
	else
		return false;
}

Re: vector配列を使って単語の長さ順にソートする

Posted: 2018年6月03日(日) 00:05
by かずま
結城紬 さんが書きました:
7年前
"vector" はクラス名としてすでに使われているので、変数名として使うことはできません。
変数名を別の名前に変えてください。
std::vector<char>::iterator p= vector.begin(); の行を削除すれば、
vector<string> vector; と書けます。
最初の vector は名前空間std内のテンプレートクラス名。
次の vector は、main関数内のローカル変数名。

Re: vector配列を使って単語の長さ順にソートする

Posted: 2018年6月03日(日) 00:53
by わりばし
結城紬 さんが書きました:
7年前
"vector" はクラス名としてすでに使われているので、変数名として使うことはできません。
変数名を別の名前に変えてください。
課題の足掛かりとして先生から配られたサンプルコードでは既に変数名がvectorだったのでそのままでした.

Re: vector配列を使って単語の長さ順にソートする

Posted: 2018年6月03日(日) 00:57
by わりばし
Bullさん
ありがとうございます,できました!
重ね重ね申し訳ないのですが,

コード:

bool precede(const std::string &str1, const std::string &str2)
{
	if (str1.length() < str2.length())
		return true;
	else
		return false;
}
の「const std::string &str1, const std::string &str2」の意味を教えていただけませんか

Re: vector配列を使って単語の長さ順にソートする

Posted: 2018年6月03日(日) 08:18
by Bull
わりばし さんが書きました:
7年前
の「const std::string &str1, const std::string &str2」の意味を教えていただけませんか
const な (変更出来ない) std::string 型の参照 str1 と str2 です。
関数の引数を参照にすると、関数内部での変更が呼び出し元に反映されます。
precede() 関数では str1 と str2 は変更されないし、変更されては困ります。
const はつけてもつけなくても、処理に変わりはありませんが、関数内で変更出来ないようにするとともに、関数の利用者に対しても変更しないことを、表明しています。

コード:

bool precede(std::string str1, std::string str2);
としてもいいのですが、値で渡すとコピーが発生して効率が悪くなるので、参照で渡しています。
この辺は今はあまり深く理解する必要はないのかもしれません、今後の学習で参照とともに理解して下さい。

Re: vector配列を使って単語の長さ順にソートする

Posted: 2018年6月03日(日) 13:49
by かずま
かずま さんが書きました:
7年前
std::vector<char>::iterator p= vector.begin(); の行を削除すれば、
vector<string> vector; と書けます。
std:: がついているので、行の削除はなくてもいいですね。
p を参照しないから削除したほうがいいんですが。

コード:

#include <iostream>    // cout, endl
#include <fstream>     // ifstream
#include <string>      // string
#include <vector>      // vector
#include <algorithm>   // sort
using namespace std;

bool precede(const string& str1, const string& str2)
{
	return str1.length() < str2.length();
}

int main()
{
	ifstream inFile("test.txt");
	if (!inFile) return cout << "can't open input file\n", 1;
	string word;
	vector<string> vector; // 以後 vector はローカル変数
	std::vector<string>::iterator p = vector.begin(); // std::vector 参照可能
	while (inFile >> word) vector.push_back(word);
	sort(vector.begin(), vector.end(), precede);
	for (unsigned i = 0; i < vector.size(); i++) cout << vector[i] << endl;
}

Re: vector配列を使って単語の長さ順にソートする

Posted: 2018年6月04日(月) 08:34
by わりばし
Bullさん
返事が遅くなってすみません。丁寧な解説、ありがとうございます!

Re: vector配列を使って単語の長さ順にソートする

Posted: 2018年6月04日(月) 08:37
by わりばし
かずまさん
pはよく分からなくて、何となく必要かな……?と思い書いていただけなので、削除しました。
ありがとうございます!