小编sbi*_*sbi的帖子

修改日志类以接受string - C++中的变量

我试图修改我的日志类以接受我的字符串中的变量.例如,如果我想输出一个区域中有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个玩家

有任何解决这个问题的方法吗?

c++ io logging

1
推荐指数
1
解决办法
259
查看次数

c ++矢量性能非直观的结果?

我正在摆弄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毫秒.这是函数调用的屏幕截图,左侧没有上面的代码行,右侧有.我真的不明白这个结果,如果你知道如果用一个吨来填充它,我学到了如何定义矢量容量对我来说没有意义是一件好事.指定保留基本上使大多数函数调用加倍.

http://imagebin.org/179302

c++ performance visual-studio-2010

1
推荐指数
1
解决办法
269
查看次数

使用std :: list编译错误

我有这个简单的函数来检查值是否在列表中:

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)

我无法弄清问题是什么.我检查过以前的问题,但没有帮助.你能解释一下我做错了什么吗?

c++ templates

1
推荐指数
1
解决办法
180
查看次数

如何将字符串读入文件C++

我将字符串写入文件有一点问题,如何将字符串写入文件并能够将其作为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)

c++

0
推荐指数
1
解决办法
487
查看次数

任何人都可以解释这个表达的含义吗?

 void* GetData()
 {
    return reinterpret_cast<unsigned char*>(this);
 }
Run Code Online (Sandbox Code Playgroud)

在这种情况下是否会发生自动类型强制的情况?我怎么能将我的类的对象转换为unsigned char*?

c++ types type-conversion

0
推荐指数
1
解决办法
263
查看次数

访问类成员时的性能

我正在写一些性能关键的东西,想知道如果我使用它是否会有所作为:

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++专家来说很明显,所以我现在不会尝试为它写一个不切实际的基准

c++ micro-optimization

0
推荐指数
1
解决办法
192
查看次数

为什么这个内部类没有编译?

这是我的代码:

#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&)

我究竟做错了什么?

c++

0
推荐指数
1
解决办法
186
查看次数

c ++中的距离计算错误

#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

0
推荐指数
2
解决办法
4796
查看次数

在那种情况下更好 - "for"或"while"

我已经读过一个地方,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循环更好?

c c++

0
推荐指数
1
解决办法
206
查看次数

构建一个金字塔,数字介于1和插入的整数之间

我正在尝试建立一个数字介于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)

如何解决这个问题,如图所示构建金字塔.

c++

0
推荐指数
1
解决办法
2300
查看次数