在VC++6中使用向量进行数组排序时出错,而在VC++2012中没有错误

error in array sorting using vectors in VC++6 while no error in VC++2012

本文关键字:出错 而在 有错误 VC++2012 VC++6 向量 数组排序      更新时间:2023-10-16

我在VC++2012中使用了这行代码来对名为arrayCosts的集成行数组进行排序。此代码在此版本中有效,但在VC++6版本中无效。

vector< vector<float> > my_vector ;
for( const auto& row : arrayCosts ) my_vector.push_back( vector<float>( begin(row), end(row) ) ) ;
        sort( begin(my_vector), end(my_vector),
                    []( const vector<float>& a, const vector<float>& b ) { return a[1] < b[1] ; } ) ;   

VC++6中的错误如下。

e:logisticsprojects1010source1.cpp(190) : error C2143: syntax error : missing ',' before ':'
e:logisticsprojects1010source1.cpp(190) : error C2530: 'row' : references must be initialized
e:logisticsprojects1010source1.cpp(190) : error C2059: syntax error : ':'
e:logisticsprojects1010source1.cpp(191) : error C2065: 'begin' : undeclared identifier
e:logisticsprojects1010source1.cpp(191) : error C2065: 'end' : undeclared identifier
e:logisticsprojects1010source1.cpp(192) : error C2059: syntax error : '['
e:logisticsprojects1010source1.cpp(192) : error C2143: syntax error : missing ')' before '{'
e:logisticsprojects1010source1.cpp(192) : error C2143: syntax error : missing ';' before '{'
e:logisticsprojects1010source1.cpp(192) : error C2065: 'a' : undeclared identifier
e:logisticsprojects1010source1.cpp(192) : error C2109: subscript requires array or pointer type
e:logisticsprojects1010source1.cpp(192) : error C2065: 'b' : undeclared identifier
e:logisticsprojects1010source1.cpp(192) : error C2109: subscript requires array or pointer type

您正试图将2011年的语言功能与1997年的编译器结合使用。这行不通。

如果你坚持使用C++98,你可能会取得更大的成功;尽管由于您的编译器早于此,所以不能保证这会起作用。

// Define a comparison functor, to replace the lambda.
// This must be defined outside a function, for arcane reasons.
struct comparator {
    bool operator()(const vector<float>& a, const vector<float>& b) {
        return a[1] < b[1];
    }
};
vector< vector<float> > my_vector ;
// Use an old-school for loop; range-based loops didn't exist back then,
// and neither did "auto" type deduction.
// (This assumes arrayCosts is also a vector<vector<float>>; 
// change the iterator type if it's something else).
for (vector< vector<float> >::const_iterator it = arrayCosts.begin();
     it != arrayCosts.end(); ++it)
{
    // std::begin and std::end didn't exist either
    my_vector.push_back( vector<float>( it->begin(), it->end() ) ) ;
}
// Again, no std::begin or std::end.
sort(my_vector.begin(), my_vector.end(), comparator());
相关文章: