首页 文章

错误:没有匹配函数来调用'std :: vector <std :: __ cxx11 :: basic_string <char >>> :: push_back(int&)'

提问于
浏览
6

我是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:注意:candidate:'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 回答

  • 1

    您正在阅读 int 变量 m 并尝试将其放入字符串向量中 . 你应该使用 std::vector<int> 代替 .

    Bottom line :您的代码需要 only one change ,最合理的一个是将 std::vector<std::string> 更改为 std::vector<int> .

相关问题