我有指向其他对象中的元素的指针.有安全感吗?我知道当需要更多空间时,向量将移动所有对象,因此指针将无效.
mylist.push_back(MyObj中(1));
if(someCond)
{_
myLastObj =&mylist.back();
}
_myLastObj 是类型的 MyObj*
如果我使用了一个向量,该对象将被移动到另一个位置,指针将指向垃圾.列表是否安全?
我想转换string为int但转换必须考虑前缀0x或0如果有的话,并将输入视为hex或oct分别.使用时istringstream,需要明确指定基数.除了明确编码检查字符之外,还有其他方法0x吗?
编辑:
转换应隐式根据前缀查找基数.就像那样int i = 0x123;.
void hello()
{
cout << "helloworld" << endl;
}
void hello(string s)
{
cout << "hello " << s << endl;
}
void doWork()
{
thread t1(static_cast<void ()>(&hello));
thread t2(static_cast<void (string)>(&hello),"bala");
t1.join();
t2.join();
}
Run Code Online (Sandbox Code Playgroud)
错误:
thread.cc|19 col 42| error: invalid static_cast from type '<unresolved overloaded function type>' to type 'void()'
thread.cc|20 col 48| error: invalid static_cast from type '<unresolved overloaded function type>' to type 'void(std::string) {aka void(std::basic_string<char>)}'
Run Code Online (Sandbox Code Playgroud)
我知道我可以使用typedef函数指针或lambda.是不是可以使用static_cast?
考虑以下两种方法将元素附加到向量中
std::vector<int> vi1(10,42), vi2;
vi2.insert(vi2.end(),vi1.begin(),vi1.end());
<OR>
std::copy(vi1.begin(),vi1.end(),std::back_inserter(vi2));
Run Code Online (Sandbox Code Playgroud)
std::copy版本看起来更干净,我不必输入vi2两次.但是因为它是一个通用的算法,而insert是一个成员函数,可能insert比std::copy它做得更好或者做同样的事情吗?
我可以自己测试,但我必须为每个模板类型的每个向量执行此操作.有人做过吗?
我正在编写一个类,其中一个函数的实现取决于用户.目前我将它作为虚函数,用户需要覆盖我的类来提供它的实现.我正在考虑使它成为一个仿函数(boost :: function)/ function_pointer,以便用户可以注册它.应用程序对性能至关重要,速度比看起来不错的类更重要.改为仿函数会带来一些性能上的好处吗?
在大多数情况下,它将是一个自由函数,因此函数指针应该没问题.但我想有些情况可能需要状态,因此它需要成为一个仿函数.
允许注册一个function_ptr或一个仿函数并根据某些bool调用合适的一个,我是否会获得任何性能优势?类似的东西.
class Myclass
{
public:
registerFuncPtr(FuncPtrSignature);
registerFunctor(boost::function func);
private:
someMethod(someArgs)
{
...
if(_isFuncPtr)
_func_ptr(args);
else
_functor(args);
...
}
bool _isFuncPtr;
FuncptrSignature _func_ptr;
boost::function _functor;
}
Run Code Online (Sandbox Code Playgroud)
更新:
我正在编写一个共享库,客户端将动态链接它.没有C++ 0x.:(
来自 boost doc,
这导致近乎最佳的代码生成; BOOST_FOREACH的性能通常在等效手动编码循环的几个百分点内.
我想使用宏和非标准typeof运算符,我们可以生成完全等效的运算符.BOOST_FOREACH的哪些特性使其不准确?
编辑:
我的版本:
#define EACH(it,v) \
for(typeof(v.begin()) it = v.begin();it != v.end(); ++it)
//use this if you want a const_iterator from a non-const container
#define CONST_EACH(it,v) \
typedef typeof(v) v_type; \
typedef const v_type& const_type; \
for(typeof(static_cast<const_type>(v).begin()) it = static_cast<const_type>(v).begin(); it != static_cast<const_type>(v).end(); ++it)
Run Code Online (Sandbox Code Playgroud)
我正在尝试编写一个没有任何开销的版本.这使用非标准typeof并给出迭代器而不是value_type.我在这里错过了什么吗?
考虑以下代码:
Bar my_f()
{
Foo f{args,to,create,foo};
Bar b{std::move(f),other,args}; //Is move here unnecessary?
// code that does not use f;
return b;
}
Run Code Online (Sandbox Code Playgroud)
是否需要编译器检查{code that does not use f}并自动f进入b?
class BlogPost(SchemaBase):
id = Column(Integer, primary_key=True)
name = Column(String, unique=True)
authors = relationship('Authors', secondary='authors_to_blog_post')
__tablename__ = 'blogpost'
class Authors(SchemaBase):
id = Column(Integer, primary_key=True)
name = Column(String, unique=True)
__tablename__ = 'author'
authors_to_blog_post = Table('authors_to_blog_post', Base.metadata,
Column('author_id', Integer, ForeignKey('author.id')),
Column('blogpost_id', Integer, ForeignKey('blogpost.id'))
)
Run Code Online (Sandbox Code Playgroud)
现在如何查询没有任何作者的所有博文?
session.query(BlogPost).filter(BlogPost.authors == [])不起作用
我按照wiki添加了一个带有设计的自定义电子邮件验证器。它有效,但每次验证都会打印两次错误,如下所示。如何解决这个问题?

更新:评论中链接的答案不起作用。仅当所有验证都在一次验证调用中完成时,它可能才有效。就我而言,一项验证是通过设计完成的,其他验证是由我添加的。需要明确的是,该模型如下所示:
class User < ActiveRecord::Base
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
# Setup accessible (or protected) attributes for your model
attr_accessible :email, :password, :password_confirmation, :remember_me
attr_accessible :email, :name
before_save do |user|
user.email = email.downcase
end
validates :name, presence: true, length: { maximum: 50 }
validates :email, email: true, presence: true, reduce: true # This causes 'Email is Invalid' to be …Run Code Online (Sandbox Code Playgroud) validation ruby-on-rails email-validation devise ruby-on-rails-3
我有一些库函数foo,它返回一个带有两位小数(代表价格)的浮点值。我必须传递给其他函数bar,该函数期望小数点为两位小数。
value = foo() # say value is 50.15
decimal_value = decimal.Decimal(value) # Not expected. decimal_value contains Decimal('50.14999999999999857891452847979962825775146484375')
bar(decimal_value) # Will not work as expected
# One possible solution
value = foo() # say value is 50.15
decimal_value = decimal.Decimal(str(round(value,2))) # Now decimal_value contains Decimal('50.15') as expected
bar(decimal_value) # Will work as expected
Run Code Online (Sandbox Code Playgroud)
如何将任意浮点数转换为带有 2 个小数位的固定小数点?并且无需使用中间字符串转换str.
我不担心性能。只是想确认中间 str 转换是否是 pythonic 方式。
# From selected answer
v = 50.15
d = Decimal(v).quantize(Decimal('1.00'))
# …Run Code Online (Sandbox Code Playgroud) c++ ×7
python ×2
c++11 ×1
decimal ×1
devise ×1
functor ×1
hex ×1
many-to-many ×1
precision ×1
python-3.x ×1
sql ×1
sqlalchemy ×1
stdlist ×1
stl ×1
validation ×1
virtual ×1