我想在C++中使用Qt做这样的事情:
int i = 5;
QString directory = ":/karim/pic" + i + ".jpg";
Run Code Online (Sandbox Code Playgroud)
在哪里+意味着我想连接字符串和整数(也就是说,directory应该是:/karim/pic5.jpg).我怎样才能做到这一点?
我可以用某种方式设计我的日志记录功能,它使用C++接受以下形式的串联字符串吗?
int i = 1;
customLoggFunction("My Integer i = " << i << ".");
Run Code Online (Sandbox Code Playgroud)
.
customLoggFunction( [...] ){
[...]
std::cout << "Debug Message: " << myLoggMessage << std::endl << std::endl
}
Run Code Online (Sandbox Code Playgroud)
编辑:
使用std :: string作为函数的属性适用于连接字符串,但是传递的非连接字符串(如customLoggFunction("example string"))会产生编译时错误,表示该函数不适用于char [].当我以下列方式重载函数时...
customLoggFunction(std::string message){...}
customLoggFunction(char message[]){...}
Run Code Online (Sandbox Code Playgroud)
...连接的字符串抓住了工作.
如何IN使用pqxxin c++for在 sql 查询中执行postgresql?我有vector<long>id,我需要更新表学生中的每一行(设置faculty_id为一些新值)。我想避免循环,faculty当我用prepared INSERT语句插入教师时得到的新值(_id)。是否可以prepared IN使用 pqxx传递如此可迭代的结构或创建查询?
void prepareChangeFaculty(connection_base &c){
const std::string sql =
"UPDATE students SET faculty_id=$2 WHERE id IN $1"; // here is a problem
c.prepare("change_faculty", sql);
}
Run Code Online (Sandbox Code Playgroud)
$1 我喜欢我需要更新的行的 id 向量
我试图在visual studio 2008中保存一系列图像,所有图像都带有"Image"的前缀.唯一的区别因素应该是它们的数量.例如,如果我要保存10张图像,那么情况就应该如此
i=1;
while(i<10)
{
cvSaveImage("G:/OpenCV/Results/Imagei.jpg",img2);
i++
//"i" is gonna be different every time
}
Run Code Online (Sandbox Code Playgroud)
所以我需要将整数与字符串连接起来...期待答案......
我试图连接字符串和整数如下:
#include "Truck.h"
#include <string>
#include <iostream>
using namespace std;
Truck::Truck (string n, string m, int y)
{
name = n;
model = m;
year = y;
miles = 0;
}
string Truck :: toString()
{
string truckString = "Manufacturer's Name: " + name + ", Model Name: " + model + ", Model Year: " + year ", Miles: " + miles;
return truckString;
}
Run Code Online (Sandbox Code Playgroud)
我收到此错误:
error: invalid operands to binary expression ('basic_string<char, std::char_traits<char>, std::allocator<char> >'
and 'int')
string …Run Code Online (Sandbox Code Playgroud) 我是 C++ 新手,这个问题对很多人来说可能看起来微不足道,但请记住,我刚刚开始学习 C++ 语言。
我已将一个变量分配x给 equal20并想将它与一个字符串连接起来。我的 C++ 代码如下。
#include <iostream>
#include <string>
#include <cmath>
using namespace std;
int main() {
int x = 20;
int y = 15;
if (x >= y) {
cout << x + " is greater than " + y;
}
}
Run Code Online (Sandbox Code Playgroud)
我的预期结果是20 is greater than 15,但结果却有些奇怪é@。我很困惑,我在 GeeksForGeeks、w3schools 或 SO 的其余部分找不到解决方案。
我知道使用cout << x << " is greater than " << y;效果很好,但我不确定为什么连接在这里不起作用。另外,为什么这些奇怪的字符会出现?
提前致谢。
(另外,请不要不回答这个问题留下一个答案。我记得开始的时候JS我问一个问题,唯一的答案是“不使用document.write …
我已经使用 cpp 很长一段时间了,我知道我们不能添加字符串和数字(因为 + 运算符没有为此重载)。但是,我看到了这样的代码。
#include <iostream>
using namespace std;
int main() {
string a = "";
a += 97;
cout << a;
}
Run Code Online (Sandbox Code Playgroud)
这输出'a',我也试过这个。
string a ="";
a=a+97;
Run Code Online (Sandbox Code Playgroud)
第二个代码给出了一个编译错误(作为 + 运算符std::string和 的无效参数 int)。我不想连接字符串和数字。有什么不同?为什么一个有效,而另一个无效?
我原以为a+=97是一样的,a=a+97但它似乎不同。
我有一系列文件(New1.BMP,New2.BMP,...,New10.BMP).
我需要创建一个存储上述文件名称的变量,然后在另一个代码中使用它.
我目前的代码:
int LengthFiles =10;
char FilePrefix[100]="New";//This is actually passed to the function. Am changing it here for simplicity
char str[200],StrNumber[1];//str holds the file name in the end. StrNumber is used to convert number value to character
SDL_Surface *DDBImage[9]; // This is a SDL (Simple DIrectMedia Library) declaration.
for (int l=0;l<LengthFiles ;l++)
{
itoa(l, StrNumber, 1);
strcat(str,FilePrefix);strcat(str,StrNumber);strcat(str,".BMP");
DDBImage[l]= SDL_DisplayFormat(SDL_LoadBMP(str));
}
Run Code Online (Sandbox Code Playgroud)
正如您可能看到的,我不知道如何用C++编写代码,我试图通过在线代码片段来完成这项工作.这是它应该如何在C/C++中工作,即即时创建变量.
我该如何最好地接近它?
我是C++的新手,正在开发一个简单的项目.基本上我遇到问题的地方是在文件名中创建一个带数字(int)的文件.在我看来,我必须首先将int转换为字符串(或char数组),然后将此新字符串与文件名的其余部分连接起来.
到目前为止,我的代码无法编译:
int n; //int to include in filename
char buffer [33];
itoa(n, buffer, 10);
string nStr = string(buffer);
ofstream resultsFile;
resultsFile.open(string("File - ") + nStr + string(".txt"));
Run Code Online (Sandbox Code Playgroud)
这会产生一些编译错误(在Linux中编译):
我在这里尝试过这样的建议:c string和int concatenation and here:在C++中将int转换为字符串的最简单方法,没有运气.
如果我使用to_string方法,我最终得到错误"to_string不是std的成员".
string words[5];
for (int i = 0; i < 5; ++i) {
words[i] = "word" + i;
}
for (int i = 0; i < 5; ++i) {
cout<<words[i]<<endl;
}
Run Code Online (Sandbox Code Playgroud)
我期望结果如下:
word1
.
.
word5
Run Code Online (Sandbox Code Playgroud)
在控制台中打印如下:
word
ord
rd
d
Run Code Online (Sandbox Code Playgroud)
有人可以告诉我这个的原因.我相信在java中它将按预期打印.