如何避免使用内联函数重复代码?
我的工作中有重复的代码,我想摆脱它,所以我知道在 C++ 中使用宏不是一个好主意,但是我必须使用内联函数,将此函数用作内联函数是个好主意吗:
list<Data>::iterator foo(int data){
if(dataExists(data)){
list<Data>::iterator i;
for(i = dataClass.begin(); i != dataClass.end(); ++i){
if(i->getData() == data){
break;
}
return i; //here I have one more problem, what can I return if data doesn't exist?
}
我觉得这个功能很不安全。我该如何改进我的代码?
I have repeating code in my work, and I want to get rid of it, so I know that in C++ it is not good idea to use macro, but instead I must use inline function, is it good idea to use this function as inline:
list<Data>::iterator foo(int data){
if(dataExists(data)){
list<Data>::iterator i;
for(i = dataClass.begin(); i != dataClass.end(); ++i){
if(i->getData() == data){
break;
}
return i; //here I have one more problem, what can I return if data doesn't exist?
}
I think that this function is very unsafe. How can I improve my code?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您在这里所做的事情已经由
std::find()
函数完成,因此最好使用它(尽管尝试自己实现这些东西来进行练习当然是可以的)。std::find()
还演示了一种指示“未找到”条件的好方法 - 如果未找到该项目,则它会返回末尾后一位的迭代器。这样,调用者就可以通过比较Data.end()
返回的迭代器来确定是否找到匹配项。What you're doing here is already done by the
std::find()
function, so it would be better to use that (although it's certainly OK to try implementing these things yourself for the exercise).std::find()
also demonstrates a good way to indicate the "not found" condition -- if the item is not found, it returns the iterator one-past-the-end. That way, the caller can determine whether a matching item was found by comparing the iterator returned withData.end()
.您发布的代码没有任何意义。有时您将
Data
用作类型,有时将其用作对象。假设它是一个对象,指示未找到某些内容的方法是返回一个指向其末尾的迭代器。但是您的代码太混乱了,如果不进行重大更改,就无法正常工作。
你不会通过在 SO 上发布问题来学习 C++ - 这是不可能的。相反,你需要阅读一本好的 C++ 教科书 - 我推荐 Accelerated C++,其中涵盖了诸如迭代器之类的内容细节。如果你继续问这样的问题,你只是在浪费你的时间,更重要的是,浪费我们的时间。
The code you posted makes no sense. at one point you use
Data
as a type, and at another as an object. Assuming it is an object, the way to indicate that something is not found would be to return an iterator pointing to its end.but your code is too confused for this to work without major changes.
You are not going to learn C++ by posting questions on SO - it simply cannot be done. Instead, you need to read a good C++ text book - I recommend Accelerated C++, which covers things like iterators in detail. If you carry on askking questions like this, you are simply wasting your time and, more importantly, ours.
我真的不知道你在这里问什么,但你的帖子中有几个误解。
第一个内联函数是您还不需要关心的特定优化。首先了解普通函数并正确理解它们。
正如另一个答案已经说过的,
std::find()
做了你似乎想做的事情。它不一定是 list 的成员才能工作,事实上现代 C++ 样式指南通常更喜欢非成员函数。现在看你的代码。我非常确定您发布的代码不是 C++ 代码,这使得很难理解您想要做什么。您使用的列表类型也不是
std::list<>
(迭代器的工作方式不同),并且您使用Data
作为变量(这不是在您的代码中定义)并作为一种类型。正如有人已经建议的那样,也许您应该开始更容易一点,或者更好,获取 对于 C++ 初学者来说,这是一本好书(我推荐《Accelerated C++》和《Programming P&P using C++》)。
I don't really know what you are asking here, but there are several misconceptions in your post.
First inline functions are a specific optimization you do not need to care about quite yet. Learn about normal functions first and understand them properly.
As another answer already said,
std::find()
does what you seem to want to do. It doesn't have to be a member of list to work, in fact modern C++ styleguides often prefer non member functions.Now to your code. I am pretty sure that the code you posted is not working C++ code, which makes it really hard to understand what you are trying to do. The list type you are using is also not a
std::list<>
(The iterators work differently), and you useData
as both a variable (which is not defined in your code) and as a type.As someone already suggested maybe you should start a little easier, or better yet, get a good book (I recommend "Accelerated C++" and "Programming P&P using C++") for C++ beginners.