我有下一个代码:
#include <iostream>
#include <algorithm>
#include <map>
#include <iterator>
//namespace std
//{
std::ostream& operator << ( std::ostream& out,
const std::pair< size_t, size_t >& rhs )
{
out << rhs.first << ", " << rhs.second;
return out;
}
//}
int main()
{
std::map < size_t, size_t > some_map;
// fill some_map with random values
for ( size_t i = 0; i < 10; ++i )
{
some_map[ rand() % 10 ] = rand() % 100;
}
// now I want to …Run Code Online (Sandbox Code Playgroud) 我喜欢STL算法,而更喜欢使用算法而不是通常的循环.
几乎所有STL算法通常用作:
std::algorithm_name( container.begin(), container.end(), ..... )
Run Code Online (Sandbox Code Playgroud)
container.begin(), container.end() - 是我项目中最受欢迎的单词之一.
有没有人有同样的问题?
你是怎么解决这个问题的?
您有什么建议可以避免这种重复?我看到了几种解决方法,但它们都有不同的限制(宏使用,与通常的指针不兼容等).
有时我需要写一些丑陋的算符,
例如
lhs.date_ < rhs.date_ ||
lhs.date_ == rhs.date_ && lhs.time_ < rhs.time_ ||
lhs.date_ == rhs.date_ && lhs.time_ == rhs.time_ && lhs.id_ < rhs.id_ .....
Run Code Online (Sandbox Code Playgroud)
它让我非常恼火.
所以我开始避免写下面的内容:
std::make_tuple( lhs.date_, lhs.time_, lhs.id_ ) <
std::make_tuple(rhs.date_, rhs.time_, rhs.id_ );
Run Code Online (Sandbox Code Playgroud)
我几乎感到高兴,但请注意,我可能使用元组而不是他们的目的让我担心.
你能否批评这个解决方案?
或者这是一个很好的做法?
你怎么避免这种比较?
更新:
感谢指向std :: tie以避免复制对象.
并感谢指出重复的问题
几乎所有的c ++项目都有复制c-tor /复制操作符/序列化方法等的类.这通常与所有成员一起做.
但有时开发人员会忘记为此功能添加新成员.
你知道任何简单的,而不是wrapp所有成员的方式会提醒开发人员做某事或在这个函数中写noop(memeber_name_).
我试图发明一些但却有错.
PS:单元测试可以防止这个问题,但我想要一些编译时间.
为什么C++标准允许以下内容?
#include <iostream>
#include <string>
int main()
{
std::string s(10, '\0'); // s.length() now is 10
std::cout << "string is " << s << ", length is " << s.length() << std::endl;
s.append(5, '\0'); // s.length() now is 15
std::cout << "string is " << s << ", length is " << s.length() << std::endl;
// the same with += char and push_back
// but:
s += "hello"; // s.length() returns 20 string is "hello"
std::cout << "string is " …Run Code Online (Sandbox Code Playgroud) 我正在尝试实现一个包含对象列表的接口类.如何使列表通用,以便实现类定义列表的类型:
public interface IEntity
{
Guid EntityID { get; set; }
Guid ParentEntityID{ get; set; }
Guid RoleId { get; set; }
void SetFromEntity();
void Save();
bool Validate();
IQueryable<T> GetAll(); // That is what I would like to do
List<Guid> Search(string searchQuery);
}
public class Dealer : IEntity
{
public IQueryable<Dealer> GetAll() { }
}
Run Code Online (Sandbox Code Playgroud) 有时我会收到从较长类型转换为较小类型的警告,例如:
void f( unsigned short i ) // f - accept any numeric type
// smaller than std::vector<>::size_type
{}
std::vector < some_type > v;
..
f ( v.size() );
Run Code Online (Sandbox Code Playgroud)
通常我使用下一个解决方案之一:
assert( v.size() <= std::numeric_limits< unsigned short >::max() );
f( static_cast< unsigned short >( v.size() ) );
Run Code Online (Sandbox Code Playgroud)
要么
f( boost::numeric_cast< unsigned short >( v.size() ) );
Run Code Online (Sandbox Code Playgroud)
但是在我目前的工作中,没有使用过,并且从上个月开始断言是不允许的.
您知道什么其他安全的方法可以抑制此警告?
有没有陷入困境的陷阱?
PS: 并不总是可以改变f的签名,有时也应该接受小数字类型.
编辑: 我想让转换尽可能安全.
STL的地图类型有下一个类型:
std::map< Key, Data, Compare, Alloc >
Run Code Online (Sandbox Code Playgroud)
作为模板参数之一,我们可以传递Compare谓词,为什么map接受这个谓词作为模板参数而不是构造函数中的对象?
它可以boost::function< bool, const T&, const T& >在构造函数中具有更灵活的界面.
当然,我强调,当STL被设计时,boost不存在,但设计师可以在boost :: function上创建类似的东西.
我相信它有一些深层次的原因.
编辑
抱歉对于虚拟问题,地图有相同的可能性:)
我的问题没有意义后你的答案.