我试图修改我的日志类以接受我的字符串中的变量.例如,如果我想输出一个区域中有7个玩家.
这是我写入日志功能:
void Log::writeSuccess(string text,...)
{
// Write the sucessfull operation to the logfile
logfile << "<---> " << text << endl;
}
Run Code Online (Sandbox Code Playgroud)
这是我的调用代码:
int playernum = 7;
errorLog.writeSuccess("There are %i players in the area", playernum);
Run Code Online (Sandbox Code Playgroud)
它最终输出到文件:该区域有%i个玩家
有任何解决这个问题的方法吗?
我正在摆弄VS2010中的性能向导,即测试仪器(函数调用次数和时序).
在了解了C++ STL中的向量之后,我决定看看有关填充100万个整数向量的性能的信息:
#include <iostream>
#include <vector>
void generate_ints();
int main() {
generate_ints();
return 0;
}
void generate_ints() {
typedef std::vector<int> Generator;
typedef std::vector<int>::iterator iter;
typedef std::vector<int>::size_type size;
Generator generator;
for (size i = 0; i != 1000000; ++i) {
generator.push_back(i);
}
}
Run Code Online (Sandbox Code Playgroud)
我得到的是:上述2402.37毫秒的经过时间.但我了解到,当向量耗尽时,向量必须调整自身大小,因为它们在内存中是连续的.因此,我认为通过对上述内容进行一次添加可以获得更好的性能:
generate.reserve(1000000);
Run Code Online (Sandbox Code Playgroud)
然而,这使程序的执行时间加倍到大约5000毫秒.这是函数调用的屏幕截图,左侧没有上面的代码行,右侧有.我真的不明白这个结果,如果你知道如果用一个吨来填充它,我学到了如何定义矢量容量对我来说没有意义是一件好事.指定保留基本上使大多数函数调用加倍.
我有这个简单的函数来检查值是否在列表中:
template <class T>
bool IsinList(list<T> l, T x)
{
for(list<T>::iterator it=list.begin(); it != list.end(); it++)
{
if (*it == x)
return true;
}
return false;
}
Run Code Online (Sandbox Code Playgroud)
我在同一个.cpp文件中使用了这个函数,如下所示:
if (!IsinList (words, temp))
goodwords.push_back(temp);
Run Code Online (Sandbox Code Playgroud)
但我收到这个错误:
'std::list' : use of class template requires template argument list
Run Code Online (Sandbox Code Playgroud)
我无法弄清问题是什么.我检查过以前的问题,但没有帮助.你能解释一下我做错了什么吗?
我将字符串写入文件有一点问题,如何将字符串写入文件并能够将其作为ascii文本查看?因为我能够在设置str的默认值时这样做,但是当我输入str数据时谢谢.
#include <iostream>
#include <fstream>
#include <cstring>
using namespace std;
int main()
{
fstream out("G://Test.txt");
if(!out) {
cout << "Cannot open output file.\n";
return 1;
}
char str[200];
cout << "Enter Customers data seperate by tab\n";
cin >> str;
cin.ignore();
out.write(str, strlen(str));
out.seekp(0 ,ios::end);
out.close();
return 0;
}
Run Code Online (Sandbox Code Playgroud) void* GetData()
{
return reinterpret_cast<unsigned char*>(this);
}
Run Code Online (Sandbox Code Playgroud)
在这种情况下是否会发生自动类型强制的情况?我怎么能将我的类的对象转换为unsigned char*?
我正在写一些性能关键的东西,想知道如果我使用它是否会有所作为:
int test( int a, int b, int c )
{
// Do millions of calculations with a, b, c
}
Run Code Online (Sandbox Code Playgroud)
要么
class myStorage
{
public:
int a, b, c;
};
int test( myStorage values )
{
// Do millions of calculations with values.a, values.b, values.c
}
Run Code Online (Sandbox Code Playgroud)
我确信这对C++专家来说很明显,所以我现在不会尝试为它写一个不切实际的基准
这是我的代码:
#include <algorithm>
class A {
void f() {
struct CompareMe {
bool operator() (int i, int j) { return i < j; }
} comp;
int a[] = {1, 2, 3, 4};
int found = std::min_element(a[0], a[3], comp);
}
}
Run Code Online (Sandbox Code Playgroud)
错误信息:
no matching function for call to ‘min_element(int&, int&, A::f()::CompareMe&)
我究竟做错了什么?
#include <iostream>
#include <cmath>
#include <vector>
using namespace std;
int square(int a){
return a*a;
}
struct Point{
int x,y;
};
int distance (const Point& a,const Point& b){
int k=(int) sqrt((float)((a.x-b.x)*(a.x-b.x))+((a.y-b.y)*(a.y-b.y)));
return k;
}
int main(){
vector<Point>a(10);
for (int i=0;i<10;i++){
cin>>a[i].x>>a[i].y;
}
int s=0;
int s1;
int k=0;
for (int i=1;i<10;i++){
s+=square(distance(a[0],a[i]));
}
for (int i=1;i<10;i++){
s1=0;
for (int j=0;j<10;j++){
s1+=square(distance(a[i],a[j]));
if (s1<s) { s=s1; k=i;}
}
}
cout<<k<<"Points are:";
cout<<a[k].x;
cout<<a[k].y;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我有以下代码,但这里是错误列表
1>------ Build started: Project: distance, …Run Code Online (Sandbox Code Playgroud) c++ compiler-errors using-directives overload-resolution name-lookup
我已经读过一个地方,for当你迭代某种数组/序列/列表/你调用它时,你应该使用一个循环,当你的循环需要在某个条件下停止时,我应该使用while循环.
那么,如果我有这样的东西怎么办?
int len = 0;
for(; s[len] != '\n'; ++len) {
// some processing
if (someCondition) {
return len;
}
}
// if done iterating, return len
return len;
Run Code Online (Sandbox Code Playgroud)
for在这种情况下可以使用循环还是while循环更好?
我正在尝试建立一个数字介于1和插入数字之间的金字塔.例如,如果我将6插入整数,则piramid将如下所示:
12345654321
234565432
3456543
45654
565
6
Run Code Online (Sandbox Code Playgroud)
我尝试使用for循环,但我得到任何一行或++数字到6.这是代码:
#include<stdio.h>
#include <iostream>
#include <conio.h>
int main()
{
int i,j,d;
std::cin >> d;
for(i=1;i<=d;i++)
{
for(j=1;j<=i;j++)
printf("%d",j);
printf("\n");
}
getch();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
如何解决这个问题,如图所示构建金字塔.