c++ - 错误 : no matching function for call to ‘std::vector<std::__cxx11::basic_string<char>>::push_back(int&)’

标签 c++ arrays vector

我是 C++ 的新手。当我运行我的代码时出现此错误:(

Big Sorting.cpp: In function ‘int main(int, const char**)’: Big Sorting.cpp:13:22: error: no matching function for call to ‘std::vector >::push_back(int&)’ v.push_back(m); ^ In file included from /usr/include/c++/8.1.1/vector:64, from Big Sorting.cpp:2: /usr/include/c++/8.1.1/bits/stl_vector.h:1074:7: note: 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: note: no known conversion for argument 1 from ‘int’ to ‘const value_type&’ {aka ‘const std::__cxx11::basic_string&’} /usr/include/c++/8.1.1/bits/stl_vector.h:1090:7: note: candidate: ‘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: note: no known conversion for argument 1 from ‘int’ to ‘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;
}

最佳答案

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

底线:您的代码只需更改一处,最合理的更改是更改std::vector<std::string>std::vector<int> .

关于c++ - 错误 : no matching function for call to ‘std::vector<std::__cxx11::basic_string<char>>::push_back(int&)’ ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51054902/

相关文章:

c++ - 对数组或 vector 进行排序和聚类

c# - 矩阵结构给出错误的输出

c++ - 如何使用适用于 C 和 C++ 的 GSOAP 访问 Amazon AWS S3?

c++ - std::end 以 char 的原始数组结尾

c++ - 我可以创建单例子类吗?

c++ - 为什么 std::vector 项目删除不会减少其容量?

dl 库的 C++ 等效 Windows

python - 运输优化(PuLP)

PHP 数组只输出第一个值

ios - 快速解析ip