web-dev-qa-db-ja.com

エラー:「std :: vector <std :: __ cxx11 :: basic_string <char>> :: Push_back(int&)」の呼び出しに一致する関数がありません

私はC++の初心者です。私のコードを実行すると、このエラーが発生しました:(

Big Sorting.cpp:関数 'int main(int、const char **)':Big Sorting.cpp:13:22:エラー: 'std :: vector> :: Push_back(int&)'の呼び出しに一致する関数がありませんv.Push_back(m); ^ /usr/include/c++/8.1.1/vector:64からインクルードされたファイル、Big Sorting.cpp:2から:/usr/include/c++/8.1.1/bits/stl_vector.h:1074:7:note :候補: 'void std :: vector <_Tp、_Alloc> :: Push_back(const value_type&)[with _Tp = std :: __ cxx11 :: basic_string; _Alloc = std :: allocator>; std :: vector <_Tp、_Alloc> :: value_type = std :: __ cxx11 :: basic_string] 'Push_back(const value_type&__x)^ ~~~~~~~~ /usr/include/c++/8.1.1/bits/ stl_vector.h:1074:7:注:引数1の 'int'から 'const value_type&'への既知の変換はありません{別名 'const std :: __ cxx11 :: basic_string&'} /usr/include/c++/8.1.1/bits /stl_vector.h:1090:7:注:候補: 'void std :: vector <_Tp、_Alloc> :: Push_back(std :: vector <_Tp、_Alloc> :: value_type &&)[with _Tp = std :: __ cxx11: :basic_string; _Alloc = std :: allocator>; std :: vector <_Tp、_Alloc> :: value_type = std :: __ cxx11 :: basic_string] 'Push_back(value_type && __x)^ ~~~~~~~~~ /usr/include/c++/8.1.1/bits/stl_vector .h:1090:7:注:引数1の 'int'から 'std :: vector> :: value_type &&'への既知の変換はありません{別名 'std :: __ cxx11 :: basic_string &&'}

ここに私のコードがあります

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

int main(int argc, char const *argv[]) {
    std::vector<std::string> v;

    int n, m;
    std::cin >> n;
    for (size_t i = 0; i < n; i++) {
        std::cin >> m;
        v.Push_back(m);
    }
    sort(v.begin(), v.end());
    for(int i = 0; i < v.size(); i++){
        std::cout << v[i] << '\n';
    }
    return 0;
}
7
Riajul kashem

int変数mを読み取り、それを文字列のベクトルに入れようとしています。代わりにstd::vector<int>を使用する必要があります。

一番下の行:コードに必要なもの1回の変更のみ、最も合理的な方法はstd::vector<std::string>std::vector<int>に変更することです。

1
r3mus n0x