Finding an item in std::vector

I want to check whether an element exists in the vector or not, so I can deal with each case. I came across this formula : #include <algorithm> if ( std::find(vector.begin(), vector.end(), item) != vector.end() ) do_this(); else do that(); I dont understand why do we need vector.end() at the end, isnt find(vector.begin(), vector.end(), item) enough to find the element ? std::find

在std :: vector中查找一个项目

我想检查vector中是否存在元素,所以我可以处理每种情况。 我遇到这个公式: #include <algorithm> if ( std::find(vector.begin(), vector.end(), item) != vector.end() ) do_this(); else do that(); 我不明白为什么我们需要vector.end()在最后,没有找到(vector.begin(),vector.end(),item)足以找到元素? std::find(vector.begin(), vector.end(), item) 将会返回一个Iterator,所以通过比较它

check if a std::vector contains a certain object?

Possible Duplicate: How to find an item in a std::vector? Is there something in <algorithm> which allows you to check if a std:: container contains something? Or, a way to make one, for example: if(a.x == b.x && a.y == b.y) return true; return false; Can this only be done with std::map since it uses keys? Thanks Checking if v contains the element x : #include <algori

检查一个std :: vector是否包含某个对象?

可能重复: 如何在std :: vector中找到一个项目? 在<algorithm>有什么可以让你检查一个std :: container是否包含某些东西? 或者,制作一个方法,例如: if(a.x == b.x && a.y == b.y) return true; return false; 这只能用std::map完成,因为它使用了键? 谢谢 检查v包含元素x : #include <algorithm> if(std::find(v.begin(), v.end(), x) != v.end()) { /* v contains x */ } else {

How to sum up elements of a C++ vector?

What are the good ways of finding the sum of all the elements in a std::vector ? Suppose I have a vector std::vector<int> vector with a few elements in it. Now I want to find the sum of all the elements. What are the different ways for the same? Actually there are quite a few methods. int sum_of_elems = 0; C++03 Classic for loop: for(std::vector<int>::iterator it = vector.b

如何总结C ++向量的元素?

寻找std::vector中所有元素总和的好方法是什么? 假设我有一个带有几个元素的向量std::vector<int> vector 。 现在我想找到所有元素的总和。 同样的不同方式有哪些? 其实有很多方法。 int sum_of_elems = 0; C ++ 03 经典循环: for(std::vector<int>::iterator it = vector.begin(); it != vector.end(); ++it) sum_of_elems += *it; 使用标准算法: #include <numeric> sum_of_elems = st

Trying to use qsort with vector

I'm trying to learn c++ and was trying using sort and qsort. sort() works just fine but qsort doesn't, I don't know why, so can you help me please this is the code I was trying to compile #include<iostream> #include<vector> #include<cstdlib> #include<ctime> #include<algorithm> using namespace std; int compvar(const void *one, const void *two) { i

试图使用qsort与矢量

我正在尝试学习c ++,并尝试使用sort和qsort。 sort()工作得很好,但qsort没有,我不知道为什么,所以你可以帮我请这是我正在编译的代码 #include<iostream> #include<vector> #include<cstdlib> #include<ctime> #include<algorithm> using namespace std; int compvar(const void *one, const void *two) { int a = *((int*)one); int b = *((int*)two); if (a<b)

Struct padding in C++

If I have a struct in C++, is there no way to safely read/write it to a file that is cross-platform/compiler compatible? Because if I understand correctly, every compiler 'pads' differently based on the target platform. No. That is not possible. It's because of lack of standardization of C++ at the binary level . Don Box writes (quoting from his book Essential COM, chapter COM

C ++中的结构填充

如果我在C ++中有一个struct ,是否没有办法将它安全地读/写到跨平台/编译器兼容的文件中? 因为如果我理解正确,每个编译器都会根据目标平台进行不同的填充。 不,那是不可能的。 这是因为在二进制级缺乏C ++的标准化 。 Don Box写道(引自他的书Essential COM,COM作为更好的C ++一章) C ++和可移植性 一旦决定将C ++类作为DLL进行发布,就会面临C ++的一个基本弱点 ,那就是缺少二进制级别的标准化 。 虽然ISO /

How to check a value like "#define VERSION 3.1.4" at compile time?

I am adding compile-time checks to my company's C++ projects to make sure the third-party libraries on all development machines and build servers are up-to-date. Most libraries define something like the following for eg version 3.1.4: #define VERSION_MAJOR 3 #define VERSION_MINOR 1 #define VERSION_BUILD 4 This is nice and easy to check using static_assert or preprocessor directives. Now

如何在编译时检查“#define VERSION 3.1.4”的值?

我将编译时检查添加到我公司的C ++项目中,以确保所有开发机器和构建服务器上的第三方库都是最新的。 对于例如版本3.1.4,大多数库定义如下所示: #define VERSION_MAJOR 3 #define VERSION_MINOR 1 #define VERSION_BUILD 4 这很好,并且易于使用static_assert或预处理器指令进行检查。 现在我正在查看定义单个宏的第三方库: #define VERSION 3.1.4 我如何在编译时验证这种宏的价值? 在C ++ 11中,我可以使用constex

Debugging GCC Compile Times

This question already has an answer here: Profiling the C++ compilation process 5 answers g++ some_file.cc -ftime-report will give you a rough estimate of time spent in different compiler phase. Most important ones in your case are name lookup and parsing. No way to get a per class/function compile time alas. STeven Watanabe has proposed a template profiler , available in boost sandbox

调试GCC编译时间

这个问题在这里已经有了答案: 剖析C ++编译过程5个答案 g ++ some_file.cc -ftime-report 会给你一个粗略的估计时间花在不同的编译阶段。 在你的情况最重要的是名称查找和解析。 无法获得每类/函数编译时间唉。 STeven Watanabe提出了一个模板分析器,可以在boost沙盒中找到,它有助于获得一个.cc中潜在实例的数量 我知道这不是你想要的,但ccache / distcc可能有助于加速编译。 同样,如果你有多核心机器,你可

export gone, exception specs deprecated. Will this affect your code?

This latest Herb Sutter trip report on the C++0x standardisation process indicates that the committee has decided to completely drop the "export" concept for templates, and to deprecate exception specifications. I think these are both good moves, but I'm interested if anyone out there has a code base where these changes will cause them sleepless nights? I've been programming

导出已消失,异常规格已弃用。 这会影响你的代码吗?

最新的Herb Sutter关于C ++ 0x标准化过程的旅行报告表明,委员会已决定完全放弃模板的“导出”概念,并废弃异常规范。 我认为这些都是很好的举措,但是我感兴趣的是,如果有人有代码基础,这些变化会导致他们不眠之夜? 自cfront 1.0开始,我一直使用C ++进行编程,我很高兴地说我从来没有编写过异常规范,也没有允许使用我负责的代码。 当他们被提出时,我打电话给Bjarne Stroustrup,喊道:“不要这样做!” 我给出了他们为

GCC 5.2 Error with cc1, cc1plus

I received the following error while making GCC 5.2 for CentOS 7. I had added a few files to make it work, libstdc++-devel, ccache along with running the 'download prerequisites' command. Below is what happened before the error message appeared. cp/cp-lang.o c-family/stub-objc.o cp/call.o cp/decl.o cp/expr.o cp/pt.o cp/typeck2.o cp/class.o cp/decl2.o cp/error.o cp/lex.o cp/parser.o

GCC 5.2错误与cc1,cc1plus

我在为CentOS 7制作GCC 5.2时收到以下错误。我已经添加了几个文件以使其工作,libstdc ++ - devel,ccache以及运行“下载先决条件”命令。 以下是错误消息出现之前发生的情况。 cp/cp-lang.o c-family/stub-objc.o cp/call.o cp/decl.o cp/expr.o cp/pt.o cp/typeck2.o cp/class.o cp/decl2.o cp/error.o cp/lex.o cp/parser.o cp/ptree.o cp/rtti.o cp/typeck.o cp/cvt.o cp/except.o cp/friend.o cp/init.o cp/method.

What is a .h.gch file?

I recently had a class project where I had to make a program with G++. I used a makefile and for some reason it occasionally left a .h.gch file behind. Sometimes, this didn't affect the compilation, but every so often it would result in the compiler issuing an error for an issue which had been fixed or which did not make sense. I have two questions: 1) What is a .h.gch file and what is o

什么是.h.gch文件?

我最近有一个课程项目,我不得不使用G ++编写程序。 我使用了一个makefile,出于某种原因它偶尔会留下一个.h.gch文件。 有时候,这不会影响编译,但是每隔一段时间就会导致编译器针对已修复或无意义的问题发布错误。 我有两个问题: 1)什么是.h.gch文件,用于什么目的? 和 2)为什么它会在没有清理时造成这样的问题? 谢谢您的帮助。 .gch文件是预编译的头文件。 如果.gch则会使用正常的头文件。 但是,如果您