在VC++6中使用向量进行数组排序时出错,而在VC++2012中没有错误
error in array sorting using vectors in VC++6 while no error in VC++2012
我在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());
相关文章:
- 为什么随机数生成器不在void函数中随机化数字,而在main函数中随机化
- 为什么在C的循环中使用printf的Rust代码不显示输出,而在C++的循环中显示std::cout
- 为什么在引用指针时将 const 放在 & 符号的左侧有效,而在右侧则无效?
- 为什么我们需要在 C++ 中检查空指针,而在 Java 中不需要?
- 为什么 'std::reference_wrapper' 在 c++17 中被弃用,而在 c++20 中被删除?
- 在for循环的某些迭代中,字符串的长度为0,而在其他迭代中则不为0
- 为什么带lcov的codecov在Travis上不能正常工作,而在我当地的Linux Mint上却不能正常工作
- 为什么在一个地方需要双倍英镑,而在这个宏观上不需要其他地方?
- 为什么在定义类之前声明类的对象会在友元类中给出错误,而在友元函数中不会出错
- 为什么我的C 代码在Linux上运行,而在Windows上不运行,是否有RAM内存的问题
- 我的代码在发布模式下不起作用,而在Qt的调试模式下工作
- 试图使循环仅检查字符串是否仅数字,而在编程中允许的C IM中允许的负数是一个类项目
- basic_string::替换的超出范围异常,而在范围内,正如调试相同参数的输出所证明的那样
- 循环在此代码中仅迭代一次,而在另一个代码中正确迭代
- 为什么对象在作为指针传递时超出范围,而在返回时则不然
- 为什么友元运算符<<在某些情况下使用,而在其他情况下不使用
- c++:为什么我在给出模板函数的所有类型参数时都会出错,而在省略参数时还好
- 一些C++代码在一个项目中出错,而在另一个项目中编译良好
- 在VC++6中使用向量进行数组排序时出错,而在VC++2012中没有错误
- 为什么在头文件中使用私有内部类作为参数会出错,而在另一种情况下在调用函数中会出错?