错误:没有匹配的函数可以调用“ std :: vector <std :: __ cxx11 :: basic_string <char>> :: push_back(int&)”

时间:2018-06-27 05:09:18

标签: c++ arrays vector

我是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:注意:候选: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&'的已知转换{aka   ‘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 &&'{aka   ‘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;
}

1 个答案:

答案 0 :(得分:1)

您正在读取int变量m,并试图将其放入字符串向量中。您应该改用std::vector<int>

底线:您的代码仅需更改,最合理的一种方法是将std::vector<std::string>更改为std::vector<int>