Kind of a basic question but I'm having troubles thinking of a solution so I need a push in the right direction. I have an input file that I'm pulling in, and I have to put it into one string variable. The problem is I need to split this string up into different things. There will be 3 strings and 1 int. They are separated by a ":". I know I can find the position of the f
这是一个基本的问题,但我在考虑解决方案时遇到麻烦,所以我需要向正确的方向推动。 我有一个输入文件,我将它放入一个字符串变量中。 问题是我需要将这个字符串分成不同的东西。 将会有3个字符串和1个int。 它们由“:”分隔。 我知道我可以通过find()找到第一个“:”的位置,但我真的不知道如何通过字符串进行处理,并将它放入它自己的字符串/ int中。 该文件的实际输入如下所示: A:PEP:909:Inventory Item A将成为
Consider the following code: struct A { void operator++() const {} }; void operator++(const A&) {} int main () { const A ca; ++ca; // g++ Error (as expected): ambiguous overload for ‘operator++’ A a; ++a; // g++ Warning: "ISO C++ says that these are ambiguous, // even though the worst conversion for the first is better // than the worst conversion fo
考虑下面的代码: struct A { void operator++() const {} }; void operator++(const A&) {} int main () { const A ca; ++ca; // g++ Error (as expected): ambiguous overload for ‘operator++’ A a; ++a; // g++ Warning: "ISO C++ says that these are ambiguous, // even though the worst conversion for the first is better // than the worst conversion for the second
I am a decent procedural programmer, but I am a newbie to object orientation (I was trained as an engineer on good old Pascal and C). What I find particularly tricky is choosing one of a number of ways to achieve the same thing. This is especially true for C++, because its power allows you to do almost anything you like, even horrible things (I guess the power/responsibility adage is appropriat
我是一位体面的程序员,但我是一位面向对象的新手(我被培养成一名优秀的Pascal和C工程师)。 我发现特别棘手的是选择其中一种方法来实现同样的目标。 对C ++来说尤其如此,因为它的强大功能可以让你做任何你喜欢的事情,甚至是可怕的事情(我想这里的权力/责任格言是适当的)。 我认为这可能会帮助我运行一个我正在与社区共同努力的特定案例,以便了解人们如何做出这些选择。 我在寻找的是与我的具体案例相关的建议,也是
Can somebody explain me how has_trivial_default_constructor works? I tried to find it in a boost implementation but unfortunately there are too many macros and I just got lost... How somebody can detect the trivial_default_constructor in C++ using templates? I need an example in C++ 03 not 11. #include <boost/type_traits.hpp> #include <boost/static_assert.hpp> struct A{ A
有人可以解释我如何has_trivial_default_constructor作品? 我试图在boost实现中找到它,但不幸的是,有太多的宏,我只是迷路了... 如何有人可以使用模板在C ++中检测trivial_default_constructor ? 我需要一个C ++ 03而不是11的例子。 #include <boost/type_traits.hpp> #include <boost/static_assert.hpp> struct A{ A(){} int a; //std::vector< int > b; }; int main(int argc,
PROBLEM I have a std::vector of a gazillion Foo's struct Foo { int m_i; char m_c; char m_padding[3]; // want to replace this }; I can fwrite this chunk of contiguous Foo's quickly in binary form all in one shot. My problem is, if I don't put in that m_padding explicitly, calculate it, and clear it myself, valgrind will complain about uninitialized writes. QUESTION
问题 我有一个gazillion Foo的std :: vector struct Foo { int m_i; char m_c; char m_padding[3]; // want to replace this }; 我可以用二进制格式快速地将这部分连续的Foo快速地写入一次。 我的问题是,如果我没有明确地写入m_padding,计算它并自己清除它,valgrind会抱怨未初始化的写入。 题 是否有可能在C ++ 11中编写模板类,它将在编译期间为我计算该填充? 如果是这样,我可以在我的所有Foo的末
What to use as replacement for concepts (upcoming feature) in C++? You might have heard of concepts in C++. It is a feature that will allow you to specify requirements on types in templates. I am looking for a way to do this now, and the best I found is in Stroustrup's book where he uses predicates together with static_assert like this: template<typename Iter, typename Val> Iter fi
在C ++中用什么来替代概念(即将发布的功能)? 你可能听说过C ++中的概念。 这是一个功能,可以让你指定模板类型的需求。 我正在寻找一种方法来实现这一点,我发现的最好的方法是在Stroustrup的书中,他使用谓词和static_assert一起使用,如下所示: template<typename Iter, typename Val> Iter find(Iter b, Iter e, Val x) { static_assert(Input_iterator<Iter>(),"find(): Iter is not a Forward iter
I know that there is no way in C++ to obtain the size of a dynamically created array, such as: int* a; a = new int[n]; What I would like to know is: Why? Did people just forget this in the specification of C++, or is there a technical reason for this? Isn't the information stored somewhere? After all, the command delete[] a; seems to know how much memory it has to release, so it seems
我知道在C ++中没有办法获得动态创建的数组的大小,例如: int* a; a = new int[n]; 我想知道的是:为什么? 人们是否在C ++规范中忘记了这一点,或者是否存在技术原因? 这些信息是否存储在某个地方? 毕竟,这个命令 delete[] a; 似乎知道它有多少内存释放,所以在我看来, delete[]有知道的大小某种方式a 。 您经常会发现内存管理器将仅分配一定数量的空间,例如64个字节。 所以,你可能会要求新的int [4],即16
I am porting some C99 code that makes heavy use of variable length arrays (VLA) to C++. I replaced the VLAs (stack allocation) with an array class that allocates memory on the heap. The performance hit was huge, a slowdown of a factor of 3.2 (see benchmarks below). What fast VLA replacement can I use in C++? My goal is to minimize performance hit when rewriting the code for C++. One idea t
我将一些C99代码移植到C ++中,这些代码大量使用了可变长度数组(VLA)。 我用一个在堆上分配内存的数组类来替换VLA(堆栈分配)。 性能影响巨大,降低了3.2倍(见下面的基准)。 我可以在C ++中使用什么样的VLA快速替换? 我的目标是在重写C ++代码时将性能降到最低。 向我提出的一个想法是编写一个在类中包含固定大小存储(即可以堆栈分配)的数组类,并将其用于小型数组,并自动切换到大型数组的堆分配。 我的这个实
As my usually used C++ compilers allow variable-length arrays (eg. arrays depending on runtime size), I wonder if there is something like std::array with variable size? Of course std::vector is of variable size, but it allocates on heap, and reallocates on need. I like to have a stack allocated array with size defined at runtime. Is there any std -template that may feature this? Maybe using
由于我通常使用的C ++编译器允许可变长度的数组(例如数组,取决于运行时的大小),我想知道是否有像可变大小的std::array这样的东西? 当然, std::vector的大小是可变的,但它在堆上分配,并根据需要重新分配。 我喜欢在运行时定义大小的堆栈分配数组。 是否有任何std模板可能具有此功能? 也许使用固定的最大尺寸的std::vector ? 目前有两个建议将运行时固定大小的数组引入到C ++中,这可能会对您感兴趣: 具有自动
I haven't used C very much in the last few years. When I read this question today I came across some C syntax which I wasn't familiar with. Apparently in C99 the following syntax is valid: void foo(int n) { int values[n]; //Declare a variable length array } This seems like a pretty useful feature. Was there ever a discussion about adding it to the C++ standard, and if so, why it
在过去的几年中我并没有使用C语言。 当我今天看到这个问题时,我遇到了一些我不熟悉的C语法。 显然,在C99中,以下语法是有效的: void foo(int n) { int values[n]; //Declare a variable length array } 这似乎是一个非常有用的功能。 有没有关于将其添加到C ++标准的讨论,如果有的话,为什么它被省略? 一些潜在的原因: 供编译器供应商实施 与标准的其他部分不兼容 功能可以用其他C ++结构模拟 C ++标