Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/algorithm/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 使用向量<;向量<;int>&燃气轮机;c++;?_C++_Algorithm_Sorting_Vector_Sum - Fatal编程技术网

C++ 使用向量<;向量<;int>&燃气轮机;c++;?

C++ 使用向量<;向量<;int>&燃气轮机;c++;?,c++,algorithm,sorting,vector,sum,C++,Algorithm,Sorting,Vector,Sum,我不熟悉向量矩阵 C++和使用向量>请 cin >> n >> m; vector<vector<int>> A(n, vector<int> (m)); for (auto& rows : A) for (auto& x : rows) cin >> x; sort(A.begin(), A.end()); cin>>n>>m;

我不熟悉向量矩阵

C++和使用向量>请

cin >> n >> m;

    vector<vector<int>> A(n, vector<int> (m));

    for (auto& rows : A)
        for (auto& x : rows)
            cin >> x;



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

cin>>n>>m;
向量A(n,向量m));
用于(自动行:A)
用于(自动和x:行)
cin>>x;
排序(A.begin(),A.end());

不过我的那一类不好。谢谢

使用头
中声明的标准算法
std::acculate
和使用该算法并将被传递到标准算法
std::sort
的lambda表达式,或者自己编写类似函数

下面是两个实现这两种方法的演示程序

#include <iostream>
#include <iomanip>
#include <vector>
#include <iterator>
#include <algorithm>
#include <numeric>
#include <cstdlib>
#include <ctime>

int main() 
{
    size_t n = 0, m = 0;

    std::cin >> n >> m;

    std::vector<std::vector<int>> v( n, std::vector<int>( m ) );

    std::srand( ( unsigned int )std::time( nullptr ) );

    for ( auto &row : v )
    {
        for ( auto &item : row )
        {
            item = std::rand() % ( n * m );
        }
    }

    for ( const auto &row : v )
    {
        for ( const auto &item : row )
        {
            std::cout << std::setw( 2 ) << item << ' ';
        }
        std::cout << '\n';
    }

    std::cout << '\n';

    auto less = []( const auto &row1, const auto &row2 )
    {
        return std::accumulate( std::begin( row1 ), std::end( row1 ), 0ll ) <
               std::accumulate( std::begin( row2 ), std::end( row2 ), 0ll );
    };

    std::sort( std::begin( v ), std::end( v ), less );

    for ( const auto &row : v )
    {
        for ( const auto &item : row )
        {
            std::cout << std::setw( 2 ) << item << ' ';
        }
        std::cout << '\n';
    }

    std::cout << '\n';

    return 0;
}

使用标头
中声明的标准算法
std::acculate
和使用该算法并将传递给标准算法
std::sort
的lambda表达式,或者自己编写类似函数

下面是两个实现这两种方法的演示程序

#include <iostream>
#include <iomanip>
#include <vector>
#include <iterator>
#include <algorithm>
#include <numeric>
#include <cstdlib>
#include <ctime>

int main() 
{
    size_t n = 0, m = 0;

    std::cin >> n >> m;

    std::vector<std::vector<int>> v( n, std::vector<int>( m ) );

    std::srand( ( unsigned int )std::time( nullptr ) );

    for ( auto &row : v )
    {
        for ( auto &item : row )
        {
            item = std::rand() % ( n * m );
        }
    }

    for ( const auto &row : v )
    {
        for ( const auto &item : row )
        {
            std::cout << std::setw( 2 ) << item << ' ';
        }
        std::cout << '\n';
    }

    std::cout << '\n';

    auto less = []( const auto &row1, const auto &row2 )
    {
        return std::accumulate( std::begin( row1 ), std::end( row1 ), 0ll ) <
               std::accumulate( std::begin( row2 ), std::end( row2 ), 0ll );
    };

    std::sort( std::begin( v ), std::end( v ), less );

    for ( const auto &row : v )
    {
        for ( const auto &item : row )
        {
            std::cout << std::setw( 2 ) << item << ' ';
        }
        std::cout << '\n';
    }

    std::cout << '\n';

    return 0;
}

您需要为
sort()
实现一个自定义比较函数,该函数通过两个向量的和来比较这两个向量。请参阅C++ C++教程,介绍使用C++库和算法的自定义比较器的例子。@ SAMVARSHIVCHIK,我只需要这个函数来积累它们。@ Mogovan,看,第三个构造函数。您希望实现一个自定义比较函数。
std::vector
的默认比较是按字典顺序进行比较的,所以您需要重写它。@JohnFilleau我现在要做一个函数,我试过了,但是没有用,如果您能帮我处理函数,那将非常有帮助。感谢您需要为您的
sort()
实现一个自定义比较函数,该函数通过两个向量的和来比较这两个向量。请参阅C++ C++教程,介绍使用C++库和算法的自定义比较器的例子。@ SAMVARSHIVCHIK,我只需要这个函数来积累它们。@ Mogovan,看,第三个构造函数。您希望实现一个自定义比较函数。
std::vector
的默认比较是按字典顺序进行比较的,所以您需要重写它。@JohnFilleau我现在要做一个函数,我试过了,但是没有用,如果您能帮我处理函数,那将非常有帮助。谢谢
 3  3  1  4 
 6  1  5  7 
 5  6  7  2 

 3  3  1  4 
 6  1  5  7 
 5  6  7  2