int i = 4;
string text = "Player ";
cout << (text + i);
Run Code Online (Sandbox Code Playgroud)
我想要它打印Player 4.
上面显然是错误的,但它显示了我在这里要做的事情.有没有一种简单的方法可以做到这一点,还是我必须开始添加新的包含?
我正在研究Visual Studio 2008中的一个大型C++项目,并且有很多文件带有不必要的#include指令.有时#includes只是工件,一切都会被删除,但是在其他情况下,类可以向前声明,#include可以移动到.cpp文件中.是否有任何好的工具可以检测这两种情况?
我试图以最好的方式迭代静态字符串数组的所有元素.我希望能够在一行上声明它并轻松添加/删除元素,而无需跟踪数字.听起来很简单,不是吗?
可能的非解决方案:
vector<string> v;
v.push_back("abc");
b.push_back("xyz");
for(int i = 0; i < v.size(); i++)
cout << v[i] << endl;
Run Code Online (Sandbox Code Playgroud)
问题 - 无法使用字符串列表在一行上创建向量
可能的非解决方案2:
string list[] = {"abc", "xyz"};
Run Code Online (Sandbox Code Playgroud)
问题 - 无法自动获取字符串数量(我知道).
必须有一个简单的方法来做到这一点.
当我的C++方法遇到奇怪但无法恢复的东西时,我想抛出异常.投掷是否可以std::string指针可以吗?
这是我期待的事情:
void Foo::Bar() {
if(!QueryPerformanceTimer(&m_baz)) {
throw new std::string("it's the end of the world!");
}
}
void Foo::Caller() {
try {
this->Bar(); // should throw
}
catch(std::string *caught) { // not quite sure the syntax is OK here...
std::cout << "Got " << caught << std::endl;
}
}
Run Code Online (Sandbox Code Playgroud) 我在C++中定义了一个接口,即一个只包含纯虚函数的类.
我想明确禁止接口的用户通过指向接口的指针删除对象,所以我为接口声明了一个受保护的非虚拟析构函数,如:
class ITest{
public:
virtual void doSomething() = 0;
protected:
~ITest(){}
};
void someFunction(ITest * test){
test->doSomething(); // ok
// deleting object is not allowed
// delete test;
}
Run Code Online (Sandbox Code Playgroud)
GNU编译器给我一个警告说:
class'ITest'具有虚函数但非虚析构函数
一旦析构函数受到保护,虚拟或非虚拟有什么区别?
你认为这个警告可以被安全地忽略或沉默吗?
我目前从一个向量写一组双打到一个文本文件,如下所示:
std::ofstream fout;
fout.open("vector.txt");
for (l = 0; l < vector.size(); l++)
fout << std::setprecision(10) << vector.at(l) << std::endl;
fout.close();
Run Code Online (Sandbox Code Playgroud)
但这需要花费大量时间才能完成.有没有更快或更有效的方法来做到这一点?我很乐意看到并学习它.
例如,如果外部文本文件中的数据是这样的:
45.78 67.90 87
34.89 346 0.98
Run Code Online (Sandbox Code Playgroud)
如何阅读此文本文件并将每个数字分配给c ++中的变量?使用ifstream,我可以打开文本文件并为变量分配第一个数字,但我不知道如何读取空格后的下一个数字.
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
float a;
ifstream myfile;
myfile.open("data.txt");
myfile >> a;
cout << a;
myfile.close();
system("pause");
return 0;
}
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
int data[6], a, b, c, d, e, f;
ifstream myfile;
myfile.open("a.txt");
for(int i = 0; i << 6; i++)
myfile >> data[i];
myfile.close();
a = data[0];
b = data[1];
c = data[2];
d = data[3]; …Run Code Online (Sandbox Code Playgroud) 是std::array<int,10>(没有我自己使用new)保证在堆栈中分配,而不是由C++ - Standard分配?
要清楚,我不是故意的new std::array<int, 10>.我主要想知道,如果允许标准库new在其实现中使用.
我正在使用QPlainTextEdit和编写自定义代码编辑器,QSyntaxHighlighter我遇到了一个小故障.我想在选择中保留语法突出显示.但是,选择的颜色(环境颜色)会覆盖由QSyntaxHighlighterhtml标记突出显示的文本的颜色.保留字体系列等其他属性.
例:
没有选择:选择:

(我想Hello变成绿色,World!变成黑色)
我也尝试将样式表设置为:
QPlainTextEdit {
selection-color: rgba(0, 0, 0, 0);
selection-background-color: lightblue;
}
Run Code Online (Sandbox Code Playgroud)
结果:

背景颜色覆盖文本,并且文本颜色alpha = 0不可见.我这样做只是为了排除语法颜色持续存在的想法selection-color.事实上它被覆盖了selection-background-color.
编辑:不,如果我也设置selection-background-color为rgba(0, 0, 0, 0),则没有选择,并且该选择中没有文本.我所看到的只是背景.
使整个光标的线突出显示的以下片段的方法似乎是要走的路,但我基本上最终会重新实现所有的选择机制......
QList<QTextEdit::ExtraSelection> extraSelections;
QTextCursor cursor = textCursor();
QTextEdit::ExtraSelection selection;
selection.format.setBackground(lineHighlightColor_);
selection.format.setProperty(QTextFormat::FullWidthSelection, true);
selection.cursor = cursor;
selection.cursor.clearSelection();
extraSelections.append(selection);
setExtraSelections(extraSelections);
Run Code Online (Sandbox Code Playgroud)
有没有更简单的解决方案?