Easiest way to convert int to string in C++

What is the easiest way to convert from int to equivalent string in C++. I am aware of two methods. Is there any easier way? (1) int a = 10; char *intStr = itoa(a); string str = string(intStr); (2) int a = 10; stringstream ss; ss << a; string str = ss.str(); C++11 introduces std::stoi (and variants for each numeric type) and std::to_string , the counterparts of the C atoi and itoa b

将int转换为C ++字符串的最简单方法

从int转换为C ++中的等效string的最简单方法是什么? 我知道两种方法。 有没有更简单的方法? (1) int a = 10; char *intStr = itoa(a); string str = string(intStr); (2) int a = 10; stringstream ss; ss << a; string str = ss.str(); C ++ 11引入了std::stoi (以及每种数字类型的变体)和std::to_string ,C atoi和itoa的对应部分,但用std::string 。 #include <string> std::string s = std::

string is not a member of std, says g++ (mingw)

I am making a small vocabulary remembering program where words would would be flashed at me randomly for meanings. I want to use standard C++ library as Bjarne Stroustroup tells us, but I have encountered a seemingly strange problem right out of the gate. I want to change a long integer into std::string so as to be able to store it in a file. I have employed to_string() for the same. The pro

字符串不是std的成员,说g ++(mingw)

我正在制作一个小词汇记忆程序,在这个程序中,词语会随机地闪现在我的意思之中。 我想用Bjarne Stroustroup告诉我们的标准C ++库,但是我遇到了一个看起来很奇怪的问题。 我想将一个long整型变成std::string以便能够将它存储在一个文件中。 我已经用to_string()同样的to_string() 。 问题是,当我用g ++(在版本标志中提到的版本4.7.0)编译它时,它说: PS C:UsersAnuragSkyDriveCollegePrograms> g++ -std=c++0x ttd.

C++ performance challenge: integer to std::string conversion

Can anyone beat the performance of my integer to std::string code, linked below? There are already several questions that explain how to convert an integer into a std::string in C++, such as this one, but none of the solutions provided are efficient. Here is compile-ready code for some common methods to compete against: The "C++ way", using stringstream: http://ideone.com/jh3Sa

C ++性能挑战:整数到std :: string的转换

任何人都可以击败我的整数到std ::字符串代码的性能,下面链接? 已经有几个问题解释了如何在C ++中将整数转换为std::string ,比如这个,但是没有一个提供的解决方案是有效的。 以下是一些编译就绪代码,用于与一些常见方法进行竞争: 使用stringstream的“C ++方式”:http://ideone.com/jh3Sa sprintf,SO-ers通常会向性能敏感的用户推荐:http://ideone.com/82kwR 与流行的观点相反, boost::lexical_cast有它自己的

Example of increasing the work per thread in CUDA

Intro : First and as an introduction, i am quite "proud" to ask my first question on StackOverflow. I hope I'll be able to help other people as much as they help me. Algorithm : I'm writing a program with CUDA and the problem is the following: Two matrices A (n * 128) and B (m * 128) I take the first row of A, and I compute the distance between that vector and all the r

增加CUDA中每个线程的工作量的示例

简介 :首先,作为一个介绍,我很自豪地问我关于StackOverflow的第一个问题。 我希望我能够帮助其他人,就像他们帮助我一样。 算法 : 我正在用CUDA编写一个程序,问题如下: 两个矩阵A(n * 128)和B(m * 128) 我拿A的第一行,然后我逐一计算该向量和B的所有行之间的距离。 我将每个距离的结果写入矩阵C的一行,因此C的元素C(i,j)包含A的行i和行B的距离。 我继续下一行A. 我已经这样实现了:我有一个由(n

Why does C++ output negative numbers when using modulo?

Math : If you have an equation like this: x = 3 mod 7 x could be ... -4, 3, 10, 17, ..., or more generally: x = 3 + k * 7 where k can be any integer. I don't know of a modulo operation is defined for math, but the factor ring certainly is. Python : In Python, you will always get non-negative values when you use % with a positive m : #!/usr/bin/python # -*- coding: utf-8 -*- m = 7

为什么C ++在使用模数时输出负数?

数学 : 如果你有这样的等式: x = 3 mod 7 x可以是...... -4,3,10,17 ......或者更一般地说: x = 3 + k * 7 其中k可以是任何整数。 我不知道模数运算是为数学定义的,但是这个因素肯定是。 Python : 在Python中,当您使用具有正m %时,您将始终获得非负值: #!/usr/bin/python # -*- coding: utf-8 -*- m = 7 for i in xrange(-8, 10 + 1): print(i % 7) 结果是: 6 0 1 2 3 4 5 6

Resource that briefly describe the C and C++ standards

After having an answer here wrong, because I wasn't up to date on the C standard, I started to look for some place that gives a description of whats in the C and C++ standards. I do not want the complete standards, of which I found links in Where do I find the current C or C++ standard documents?, or intimate technical discussions. Instead I would like something that briefly describes the

简要描述C和C ++标准的资源

在回答这里错误之后,因为我没有及时了解C标准,所以我开始寻找一些可以描述C和C ++标准内容的地方。 我不想要完整的标准,其中我在哪里可以找到当前C或C ++标准文档的链接,或者亲密的技术讨论。 相反,如果我想更彻底地检查它,并且可能会说明引入了哪个标准,我希望简要描述该标准的内容,并参考实际标准。 有这样的资源吗? 编辑 :这个问题的一个小背景:我已经编程C超过20年了,当我知道它没有太多标准化。 官方标

How would the standard allow indirection through a null pointer?

EDIT: This is related to active issue 232 on the C++ Standard Core Language Active Issues So the real question is how would the standard allow indirection through a null pointer? Consider the following code: struct a { int x; }; struct b { int y; }; struct c: a, b { }; b *f(c *pointer_to_c) { return pointer_to_c; } f(...) has to test if pointer_to_c is NULL and return NULL if it is. (A N

标准将如何通过空指针进行间接寻址?

编辑:这与C ++标准核心语言活动问题上的活动问题232有关 所以真正的问题是标准如何允许通过空指针间接寻址? 考虑下面的代码: struct a { int x; }; struct b { int y; }; struct c: a, b { }; b *f(c *pointer_to_c) { return pointer_to_c; } f(...)必须测试pointer_to_c是否为NULL ,如果是,则返回NULL 。 ( NULL指针总是一个NULL指针,不管你如何转换它。) 现在,考虑以下几点: b *f_ref(c &reference_

Where do I find the current C or C++ standard documents?

For many questions the answer seems to be found in "the standard". However, where do we find that? Preferably online. Googling can sometimes feel futile, again especially for the C standards, since they are drowned in the flood of discussions on programming forums. To get this started, since these are the ones I am searching for right now, where are there good online resources for

我在哪里可以找到当前的C或C ++标准文档?

对于许多问题,答案似乎在“标准”中找到。 但是,我们在哪里可以找到? 最好在线。 谷歌搜索有时候会觉得徒劳无功,尤其是C标准,因为他们在编程论坛的大量讨论中被淹没了。 为了做到这一点,因为这些是我正在寻找的那些,那里有良好的在线资源: C89 C99 C11 C ++ 98 C ++ 03 C ++ 11 C ++ 14 该标准的PDF版本 截至2014年9月1日,PDF格式的C和C ++标准文档的最佳价格位置为: C ++ 14 - ISO / IEC 1

return by reference or pointer and check for null?

I have a class : class A { private: vector<int> x; public: const vector<int>& immutable_data() { return x; } vector<int>* mutable_data() { return &x; } } Now if i use this class , in my code , do i have to check if the pointer returned by the mutable_data() is null or not (given that i know the structure of this class).

通过引用或指针返回并检查null?

I have a class : class A { private: vector<int> x; public: const vector<int>& immutable_data() { return x; } vector<int>* mutable_data() { return &x; } } 现在,如果我使用这个类,在我的代码中,是否必须检查mutable_data()返回的指针是否为null(假定我知道这个类的结构)。 我个人认为我不需要,因为我知道存在一个辅助api,它返

C++ Passing `this` into method by reference

I have a class constructor that expects a reference to another class object to be passed in as an argument. I understand that references are preferable to pointers when no pointer arithmetic will be performed or when a null value will not exist. This is the header declaration of the constructor: class MixerLine { private: MIXERLINE _mixerLine; public: MixerLine(const MixerDevice&am

C ++通过引用将`this`传入方法

我有一个类构造函数,期望引用另一个类对象作为参数传入。 我明白,当没有指针算术执行或空值不存在时,引用优于指针。 这是构造函数的头声明: class MixerLine { private: MIXERLINE _mixerLine; public: MixerLine(const MixerDevice& const parentMixer, DWORD destinationIndex); ~MixerLine(); } 这是调用构造函数(MixerDevice.cpp)的代码: void MixerDevice::enumerateLines() { DWORD