vector<string> のソートの仕方 (STL)

フォーラム(掲示板)ルール
フォーラム(掲示板)ルールはこちら  ※コードを貼り付ける場合は [code][/code] で囲って下さい。詳しくはこちら
dic
記事: 656
登録日時: 13年前
住所: 宮崎県
連絡を取る:

vector<string> のソートの仕方 (STL)

#1

投稿記事 by dic » 3年前

vector<string> g_vLink1 の内容をソートするコードはどうやればいいのでしょうか?

https://cpprefjp.github.io/reference/al ... /sort.html
ここを参考に

コード:

sort( g_vLink1.begin(), g_vLink1.end() );
としたのですが、コンパイラにひっかかります。

コンパイラはVisual C++ 2008 express を使用しています。

コード:

sort( g_vLink1 );
としたら、引数は3ついるというエラー内容が返ってきました。

どうすればいいでしょうか?

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

Re: vector<string> のソートの仕方 (STL)

#2

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

足りないところを補ってみましたが、ちゃんと動きそうに思えます。

コード:

#include <iostream>
#include <vector>
#include <string>
#include <algorithm>

using std::vector;
using std::string;
using std::sort;

int main(void) {
    vector<string> g_vLink1;

    g_vLink1.push_back("cat");
    g_vLink1.push_back("dog");
    g_vLink1.push_back("rabbit");
    g_vLink1.push_back("pig");
    g_vLink1.push_back("cow");

    sort( g_vLink1.begin(), g_vLink1.end() );

    for (vector<string>::iterator it = g_vLink1.begin(); it != g_vLink1.end(); it++) {
        std::cout << *it << '\n';
    }
    return 0;
}
コンパイラに渡しているコード全体と、エラーメッセージを教えてください。
複雑な問題?マシンの性能を上げてOpenMPで殴ればいい!(死亡フラグ)

dic
記事: 656
登録日時: 13年前
住所: 宮崎県
連絡を取る:

Re: vector<string> のソートの仕方 (STL)

#3

投稿記事 by dic » 3年前

コード:


#include <windows.h>
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

void	Step2()
{
	vector<string> vTextAll;

	int i;
	for( i=0; i<DEF_NUM; i++ )
	{
		char szFile[256];
		sprintf_s( szFile, "text%d.txt", i );

		FILE	*file = NULL;
		fopen_s( &file, szFile, "rt" );
		if( file != NULL )
		{
			char	szBuf[8000];
			while( !feof(file) )
			{
				memset( szBuf, 0, sizeof(szBuf) );
				fgets( szBuf, sizeof(szBuf), file );
				vTextAll.push_back( szBuf );
			}
			fclose(file);
			file = NULL;
		}
	}

	unsigned int j;
	for( j=0; j<vTextAll.size(); j++ )
	{
		char szBuf[8000];
		char szBuf2[8000];
		strcpy_s( szBuf, vTextAll.at(j).c_str() );
		if( strstr(szBuf,"<div class=\"home-img\"><a href=\"https://tsundora.com/") )
		{
			memset( szBuf2, 0, sizeof(szBuf2) );
			char *s, *e;
			s = strstr( szBuf, "https://tsundora.com" );
			e = strstr( s, "\"" );
			for( int k=0; k+s!=e; k++ )
			{
				szBuf2[k] = *(s+k);
			}
			g_vLink1.push_back( szBuf2 );
		}
	}

//	sort( g_vLink1.begin(), g_vLink1.end() );

}
コンパイルエラーメッセージです。

1>コンパイルしています...
1>ツンドラから艦これの画像ダウンロードツール.cpp
1>c:\program files (x86)\microsoft visual studio 9.0\vc\include\algorithm(3032) : error C2784: 'bool std::operator <(const std::vector<_Ty,_Alloc> &,const std::vector<_Ty,_Alloc> &)' : テンプレート 引数を 'const std::vector<_Ty,_Alloc> &' に対して 'std::basic_string<_Elem,_Traits,_Ax>' から減少できませんでした
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>,
1> _Ax=std::allocator<char>
1> ]
1> c:\program files (x86)\microsoft visual studio 9.0\vc\include\vector(1322) : 'std::operator <' の宣言を確認してください。
1> c:\program files (x86)\microsoft visual studio 9.0\vc\include\algorithm(3087) : コンパイルされたクラスの テンプレート のインスタンス化 'std::pair<_Ty1,_Ty2> std::_Unguarded_partition<_RanIt>(_RanIt,_RanIt)' の参照を確認してください
1> with
1> [
1> _Ty1=std::_Vector_iterator<std::string,std::allocator<std::string>>,
1> _Ty2=std::_Vector_iterator<std::string,std::allocator<std::string>>,
1> _RanIt=std::_Vector_iterator<std::string,std::allocator<std::string>>
1> ]
1> c:\program files (x86)\microsoft visual studio 9.0\vc\include\algorithm(3115) : コンパイルされたクラスの テンプレート のインスタンス化 'void std::_Sort<std::_Vector_iterator<_Ty,_Alloc>,__w64 int>(_RanIt,_RanIt,_Diff)' の参照を確認してください
1> with
1> [
1> _Ty=std::string,
1> _Alloc=std::allocator<std::string>,
1> _RanIt=std::_Vector_iterator<std::string,std::allocator<std::string>>,
1> _Diff=__w64 int
1> ]
1> c:\users\user\documents\visual studio 2008\projects\ツンドラから艦これの画像ダウンロードツール - コピー\ツンドラから艦これの画像ダウンロードツール\ツンドラから艦これの画像ダウンロードツール.cpp(104) : コンパイルされたクラスの テンプレート のインスタンス化 'void std::sort<std::_Vector_iterator<_Ty,_Alloc>>(_RanIt,_RanIt)' の参照を確認してください
1> with
1> [
1> _Ty=std::string,
1> _Alloc=std::allocator<std::string>,
1> _RanIt=std::_Vector_iterator<std::string,std::allocator<std::string>>
1> ]
1>c:\program files (x86)\microsoft visual studio 9.0\vc\include\algorithm(3032) : error C2784: 'bool std::operator <(const std::vector<_Ty,_Alloc> &,const std::vector<_Ty,_Alloc> &)' : テンプレート 引数を 'const std::vector<_Ty,_Alloc> &' に対して 'std::basic_string<_Elem,_Traits,_Ax>' から減少できませんでした
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>,
1> _Ax=std::allocator<char>
1> ]
1> c:\program files (x86)\microsoft visual studio 9.0\vc\include\vector(1322) : 'std::operator <' の宣言を確認してください。
1>c:\program files (x86)\microsoft visual studio 9.0\vc\include\algorithm(3032) : error C2784: 'bool std::operator <(const std::vector<_Ty,_Alloc> &,const std::vector<_Ty,_Alloc> &)' : テンプレート 引数を 'const std::vector<_Ty,_Alloc> &' に対して 'std::basic_string<_Elem,_Traits,_Ax>' から減少できませんでした
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>,
1> _Ax=std::allocator<char>
1> ]
1> c:\program files (x86)\microsoft visual studio 9.0\vc\include\vector(1322) : 'std::operator <' の宣言を確認してください。
1>c:\program files (x86)\microsoft visual studio 9.0\vc\include\algorithm(3032) : error C2784: 'bool std::operator <(const std::vector<_Ty,_Alloc> &,const std::vector<_Ty,_Alloc> &)' : テンプレート 引数を 'const std::vector<_Ty,_Alloc> &' に対して 'std::basic_string<_Elem,_Traits,_Ax>' から減少できませんでした
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>,
1> _Ax=std::allocator<char>
1> ]
1> c:\program files (x86)\microsoft visual studio 9.0\vc\include\vector(1322) : 'std::operator <' の宣言を確認してください。
1>c:\program files (x86)\microsoft visual studio 9.0\vc\include\algorithm(3032) : error C2784: 'bool std::operator <(const std::reverse_iterator<_RanIt> &,const std::reverse_iterator<_RanIt2> &)' : テンプレート 引数を 'const std::reverse_iterator<_RanIt> &' に対して 'std::basic_string<_Elem,_Traits,_Ax>' から減少できませんでした
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>,
1> _Ax=std::allocator<char>
1> ]
1> c:\program files (x86)\microsoft visual studio 9.0\vc\include\xutility(2262) : 'std::operator <' の宣言を確認してください。
1>c:\program files (x86)\microsoft visual studio 9.0\vc\include\algorithm(3032) : error C2784: 'bool std::operator <(const std::reverse_iterator<_RanIt> &,const std::reverse_iterator<_RanIt2> &)' : テンプレート 引数を 'const std::reverse_iterator<_RanIt> &' に対して 'std::basic_string<_Elem,_Traits,_Ax>' から減少できませんでした
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>,
1> _Ax=std::allocator<char>
1> ]
1> c:\program files (x86)\microsoft visual studio 9.0\vc\include\xutility(2262) : 'std::operator <' の宣言を確認してください。
1>c:\program files (x86)\microsoft visual studio 9.0\vc\include\algorithm(3032) : error C2784: 'bool std::operator <(const std::reverse_iterator<_RanIt> &,const std::reverse_iterator<_RanIt2> &)' : テンプレート 引数を 'const std::reverse_iterator<_RanIt> &' に対して 'std::basic_string<_Elem,_Traits,_Ax>' から減少できませんでした
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>,
1> _Ax=std::allocator<char>
1> ]
1> c:\program files (x86)\microsoft visual studio 9.0\vc\include\xutility(2262) : 'std::operator <' の宣言を確認してください。
1>c:\program files (x86)\microsoft visual studio 9.0\vc\include\algorithm(3032) : error C2784: 'bool std::operator <(const std::reverse_iterator<_RanIt> &,const std::reverse_iterator<_RanIt2> &)' : テンプレート 引数を 'const std::reverse_iterator<_RanIt> &' に対して 'std::basic_string<_Elem,_Traits,_Ax>' から減少できませんでした
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>,
1> _Ax=std::allocator<char>
1> ]
1> c:\program files (x86)\microsoft visual studio 9.0\vc\include\xutility(2262) : 'std::operator <' の宣言を確認してください。
1>c:\program files (x86)\microsoft visual studio 9.0\vc\include\algorithm(3032) : error C2784: 'bool std::operator <(const std::_Revranit<_RanIt,_Base> &,const std::_Revranit<_RanIt2,_Base2> &)' : テンプレート 引数を 'const std::_Revranit<_RanIt,_Base> &' に対して 'std::basic_string<_Elem,_Traits,_Ax>' から減少できませんでした
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>,
1> _Ax=std::allocator<char>
1> ]
1> c:\program files (x86)\microsoft visual studio 9.0\vc\include\xutility(2072) : 'std::operator <' の宣言を確認してください。
1>c:\program files (x86)\microsoft visual studio 9.0\vc\include\algorithm(3032) : error C2784: 'bool std::operator <(const std::_Revranit<_RanIt,_Base> &,const std::_Revranit<_RanIt2,_Base2> &)' : テンプレート 引数を 'const std::_Revranit<_RanIt,_Base> &' に対して 'std::basic_string<_Elem,_Traits,_Ax>' から減少できませんでした
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>,
1> _Ax=std::allocator<char>
1> ]
1> c:\program files (x86)\microsoft visual studio 9.0\vc\include\xutility(2072) : 'std::operator <' の宣言を確認してください。
1>c:\program files (x86)\microsoft visual studio 9.0\vc\include\algorithm(3032) : error C2784: 'bool std::operator <(const std::_Revranit<_RanIt,_Base> &,const std::_Revranit<_RanIt2,_Base2> &)' : テンプレート 引数を 'const std::_Revranit<_RanIt,_Base> &' に対して 'std::basic_string<_Elem,_Traits,_Ax>' から減少できませんでした
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>,
1> _Ax=std::allocator<char>
1> ]
1> c:\program files (x86)\microsoft visual studio 9.0\vc\include\xutility(2072) : 'std::operator <' の宣言を確認してください。
1>c:\program files (x86)\microsoft visual studio 9.0\vc\include\algorithm(3032) : error C2784: 'bool std::operator <(const std::_Revranit<_RanIt,_Base> &,const std::_Revranit<_RanIt2,_Base2> &)' : テンプレート 引数を 'const std::_Revranit<_RanIt,_Base> &' に対して 'std::basic_string<_Elem,_Traits,_Ax>' から減少できませんでした
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>,
1> _Ax=std::allocator<char>
1> ]
1> c:\program files (x86)\microsoft visual studio 9.0\vc\include\xutility(2072) : 'std::operator <' の宣言を確認してください。
1>c:\program files (x86)\microsoft visual studio 9.0\vc\include\algorithm(3032) : error C2784: 'bool std::operator <(const std::pair<_Ty1,_Ty2> &,const std::pair<_Ty1,_Ty2> &)' : テンプレート 引数を 'const std::pair<_Ty1,_Ty2> &' に対して 'std::basic_string<_Elem,_Traits,_Ax>' から減少できませんでした
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>,
1> _Ax=std::allocator<char>
1> ]
1> c:\program files (x86)\microsoft visual studio 9.0\vc\include\utility(99) : 'std::operator <' の宣言を確認してください。
1>c:\program files (x86)\microsoft visual studio 9.0\vc\include\algorithm(3032) : error C2784: 'bool std::operator <(const std::pair<_Ty1,_Ty2> &,const std::pair<_Ty1,_Ty2> &)' : テンプレート 引数を 'const std::pair<_Ty1,_Ty2> &' に対して 'std::basic_string<_Elem,_Traits,_Ax>' から減少できませんでした
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>,
1> _Ax=std::allocator<char>
1> ]
1> c:\program files (x86)\microsoft visual studio 9.0\vc\include\utility(99) : 'std::operator <' の宣言を確認してください。
1>c:\program files (x86)\microsoft visual studio 9.0\vc\include\algorithm(3032) : error C2784: 'bool std::operator <(const std::pair<_Ty1,_Ty2> &,const std::pair<_Ty1,_Ty2> &)' : テンプレート 引数を 'const std::pair<_Ty1,_Ty2> &' に対して 'std::basic_string<_Elem,_Traits,_Ax>' から減少できませんでした
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>,
1> _Ax=std::allocator<char>
1> ]
1> c:\program files (x86)\microsoft visual studio 9.0\vc\include\utility(99) : 'std::operator <' の宣言を確認してください。
1>c:\program files (x86)\microsoft visual studio 9.0\vc\include\algorithm(3032) : error C2784: 'bool std::operator <(const std::pair<_Ty1,_Ty2> &,const std::pair<_Ty1,_Ty2> &)' : テンプレート 引数を 'const std::pair<_Ty1,_Ty2> &' に対して 'std::basic_string<_Elem,_Traits,_Ax>' から減少できませんでした
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>,
1> _Ax=std::allocator<char>
1> ]
1> c:\program files (x86)\microsoft visual studio 9.0\vc\include\utility(99) : 'std::operator <' の宣言を確認してください。
1>c:\program files (x86)\microsoft visual studio 9.0\vc\include\algorithm(3032) : error C2676: 二項演算子 '<' : 'std::basic_string<_Elem,_Traits,_Ax>' は、この演算子または定義済の演算子に適切な型への変換の定義を行いません。(新しい動作; ヘルプを参照)
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>,
1> _Ax=std::allocator<char>
1> ]
1>c:\program files (x86)\microsoft visual studio 9.0\vc\include\algorithm(3032) : fatal error C1903: 直前のエラーを修復できません。コンパイルを中止します。
1>ビルドログは "file://c:\Users\user\Documents\Visual Studio 2008\Projects\ツンドラから艦これの画像ダウンロードツール - コピー\ツンドラから艦これの画像ダウンロードツール\Debug\BuildLog.htm" に保存されました。
1>ツンドラから艦これの画像ダウンロードツール - エラー 18、警告 0
========== ビルド: 0 正常終了、1 失敗、0 更新不要、0 スキップ ==========

YuO
記事: 947
登録日時: 13年前
住所: 東京都世田谷区

Re: vector<string> のソートの仕方 (STL)

#4

投稿記事 by YuO » 3年前

<string>がインクルードされていません。
std::stringを使う場合、<string>をインクルードする必要があります。

アバター
あたっしゅ
記事: 663
登録日時: 13年前
住所: 東京23区
連絡を取る:

Re: vector<string> のソートの仕方 (STL)

#5

投稿記事 by あたっしゅ » 3年前

コード:

//#include <windows.h>
#include <iostream>
#include <vector>
#include <algorithm>
    
#include <string>
#include <string.h>
    
#define onlineDBG
const int DEF_NUM = 1;
    
using namespace std;
    
vector<string> g_vTextAll;
char szBuf[8000];
char szBuf2[8000];

//void	Step2()
int
main()
{
      vector<string> vTextAll;
    
      int i;
      for( i=0; i<DEF_NUM; i++ )
      {
        char szFile[256];
    
 #ifndef onlineDBG
        sprintf_s( szFile, "text%d.txt", i );
    
        FILE	*file = NULL;
        fopen_s( &file, szFile, "rt" );
        if( file != NULL )
        {
          char	szBuf[8000];
          while( !feof(file) )
          {
            memset( szBuf, 0, sizeof(szBuf) );
            fgets( szBuf, sizeof(szBuf), file );
            vTextAll.push_back( szBuf );
          }
          fclose(file);
          file = NULL;
        }
 #endif
      }
 #ifdef onlineDBG
        vTextAll.push_back("<div class=\"home-img\"><a href=\"https://tsundora.com/cat");
        vTextAll.push_back("<div class=\"home-img\"><a href=\"https://tsundora.com/dog");
        vTextAll.push_back("<div class=\"home-img\"><a href=\"https://tsundora.com/rabbit");
        vTextAll.push_back("<div class=\"home-img\"><a href=\"https://tsundora.com/pig");
        vTextAll.push_back("<div class=\"home-img\"><a href=\"https://tsundora.com/cow");
 #endif
        for (vector<string>::iterator it = vTextAll.begin(); it != vTextAll.end(); it++) {
            std::cout << *it << '\n';
        }
        std::cout << endl;

        unsigned int j;
        for( j=0; j<vTextAll.size(); j++ )
        {
          //char szBuf[8000];
          //char szBuf2[8000];
          //strcpy_s( szBuf, vTextAll.at(j).c_str() );
          strcpy( szBuf, vTextAll.at(j).c_str() );
          if( strstr(szBuf,"<div class=\"home-img\"><a href=\"https://tsundora.com/") )
          {
            memset( szBuf2, 0, sizeof(szBuf2) );

            char *s, *e;
            s = strstr( szBuf, "https://tsundora.com" );
            e = strstr( s, "\"" );
            if( ( s != NULL )&&( e != NULL ) ) {
              for( int k=0; k+s!=e; k++ )
              {
                szBuf2[k] = *(s+k);
              }
              printf( "%d: %s\n", __LINE__, szBuf2 );
              g_vTextAll.push_back( szBuf2 );
            }
           }
        }
        for (vector<string>::iterator it = g_vTextAll.begin(); it != g_vTextAll.end(); it++) {
            std::cout << *it << '\n';
        }
        std::cout << endl;
        sort( g_vTextAll.begin(), g_vTextAll.end() );
        for (vector<string>::iterator it = g_vTextAll.begin(); it != g_vTextAll.end(); it++) {
            std::cout << *it << '\n';
        }
        return 0; // 追加
    }
// end.
ちょっと加工して https://www.onlinegdb.com/ で動くようにしたが、

コード:

            s = strstr( szBuf, "https://tsundora.com" );
            e = strstr( s, "\"" );
このへん、ロジックがおかしくないか ?
VTuber:
東上☆海美☆(とうじょう・うみみ)
http://atassyu.php.xdomain.jp/vtuber/index.html
レスがついていないものを優先して、レスするみみ。時々、見当外れなレスしみみ。

中の人:
手提鞄あたッしュ、[MrAtassyu] 手提鞄屋魚有店
http://ameblo.jp/mratassyu/
Pixiv: 666303
Windows, Mac, Linux, Haiku, Raspbery Pi, Jetson Nano, 電子ブロック 持ち。

dic
記事: 656
登録日時: 13年前
住所: 宮崎県
連絡を取る:

Re: vector<string> のソートの仕方 (STL)

#6

投稿記事 by dic » 3年前

#include <string>
を追加することで sort 関数がうまくコンパイルされました。
ありがとうございました。

返信

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