在 c++ 中连接变量
Concatenating variables in c++
我的问题是这样的:假设我有 3 个变量(i = 1,j = 2,k =3(我想这样做:"a = 0.ijk",所以 a == 0.123但是这些变量在多个 for 下,所以我将它们的值保存在数组中。
我尝试使用 sstream 将它们从字符串转换为 int (123(,然后我会将其除以 1000 得到 0.123,但它不起作用......
float arrayValores[ maxValores ];
ostringstream arrayOss[ maxValores ];
。
arrayOss[indice] << i << j << k;
istringstream iss(arrayOss[indice].str());
iss >> arrayValores[indice];
arrayValores[indice] = ((arrayValores[indice])/1000)*(pow(10,z)) ;
cout << arrayValores[indice] << " ";
indice++;
有人可以帮助我吗?
我对
你在问什么感到困惑,但这是你想做的吗?
#include <iostream>
#include <string>
using namespace std;
int main()
{
int i = 1;
int j = 2;
int k = 3;
// Create a string of 0.123
string numAsString = "0." + to_string(i) + to_string(j) + to_string(k);
cout << numAsString << 'n';
// Turn the string into a floating point number 0.123
double numAsDouble = stod(numAsString);
cout << numAsDouble << 'n';
// Multiply by 1000 to get 123.0
numAsDouble *= 1000.0;
cout << numAsDouble << 'n';
// Turn the floating point number into an int 123
int numAsInt = numAsDouble;
cout << numAsInt << 'n';
return 0;
}
这是你想要实现的吗?
#include <iostream>
#include <string>
#include <cmath>
#include <vector>
int main() {
// your i, j, k variables
std::vector<int> v = {1, 2, 3};
double d = 0;
for (int i = 0; i < v.size(); i++) {
// sum of successive powers: 1x10^-1 + 2x10^-2 + ...
d += pow(10, -(i + 1)) * v[i];
}
std::cout << d << "n";
}
相关文章:
- 连接 LPCSTR 变量和文字?
- 正在连接的等待条件变量的线程会发生什么情况?
- 将 int 转换为字符串,然后连接另一个变量以创建完整扩展名,然后将其转换为 const_char*
- 如何在 C 宏中将变量字符串与文字字符串连接起来?
- QT连接:使用lambda表达式重置通过了int变量(怪异)
- 在 c++ 中连接变量
- 动态变量名称构建,连接
- 为什么两个字符串文字可以连接,而不能连接两个字符串变量
- 声明连接变量
- C++中将不同类型的变量列表连接到逗号分隔字符串的最快方法是什么?
- C++:: 如何使用 sqlite3 insertquery 连接变量
- 将字符串变量与整数和文本字符串连接起来的 C++ 字符串流
- 将 int 变量与字符串连接会导致奇怪的输出
- Wt Dbo MySQL 后端连接错误"SET storage_engine=INNODB;":未知的系统变量'storage_engine'
- 除了字符串流,还有其他方法吗?连接一个由多种类型的变量组成的字符串
- 如何将整数变量连接到字符串文字
- GCC如何连接多个c++ std::string变量
- 在函数参数中连接字符串和变量
- 如何将4个UINT8变量连接成一个UINT32变量?
- 如何在c++中连接两个结构体类型变量