小编cpx*_*cpx的帖子

在集合或列表中按顺序查找缺失的数字

如果a std::setstd::list包含一系列自然数(1,2,3 ......).标准库中是否有一个函数可以找到丢失的数字?

c++ stl list set

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

从std :: string的开头和结尾删除数字

使用像std :: find()这样的算法函数会有更好的方法吗?

std::string s = "012X012Y012";

//After erasing the numbers from beginning and end; the end result should be X012Y

size_t index = s.find_first_not_of("0123456789");
string::iterator iter_end = s.begin();
advance(iter_end, index);
s.erase(s.begin(), iter_end);

//Now std::string should be X012Y012

size_t index = s.find_last_not_of("0123456789")+1;
string::iterator iter_start = s.begin();
advance(iter_start, index);
s.erase(iter_start, s.end());

//Finally, std::string should be X012Y
Run Code Online (Sandbox Code Playgroud)

c++

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

复制构造函数不会被调用进行复制初始化或优化?

如果是复制构造函数,private那么

情况1:没有错误,编译器不关心是否在类中定义了复制构造函数.

情况2:错误,复制构造函数是私有的,当它被创建时public,它被省略.

它是否直接优化了副本而没有注意到构造函数是否已经构建private

#include <string>
using std::string;

class T
{
    string s;
    T(const T &obj):s(obj.s){}
public:
    T(const string &str):s(str){}
};

int main()
{
    T a = ("Copy Initialization");     //Case: 1

    T b = T("Copy Initialization");    //Case: 2
}
Run Code Online (Sandbox Code Playgroud)

c++ visual-c++-2010

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

抛出不同种类的异常是有用的吗?

例如std::runtime_error,std::invalid_argument在大型项目中抛出不同的异常是否有用?或者更好的方法是std::exception提供一个好的文本参数what()?什么时候可以从中派生出一些子类std::exception

入魔

c++ exception

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

WaitForMultipleObjects使c ++失败

我目前正在编写一个程序,它将同时运行多个程序,而其他程序则自行运行.

if( WAIT_FAILED == WaitForMultipleObjects(numberOfProgramsRan, &information[i].hProcess, TRUE, INFINITE) ) { wcerr << L"Failure waiting for process" << endl; }

numberOfProgramsRan是我在循环中运行的程序数. &information[i]是一个vector从创建过程中保存我的过程信息

当我在for循环中创建进程时,如果在运行我的下一个进程之前创建了两个或更少的进程(因此两个程序被传入运行),我的程序将等待.如果创建两个以上的进程(或在我的向量中传入两个以上的程序),我的WaitForMultipleObjects失败.

如果我需要进一步解释我的问题,请告诉我.

谢谢你的帮助

c++ winapi multithreading concurrent-programming

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

c ++指针和数组

#include <stdio.h>
#include <stdlib.h>
#include <iostream>
using namespace std;

int width = 100;
int height = 100;

float cam[] = {-1.1,-1.0,1.2};
float D[] = {0.2,0.2,-0.2};
float U[] = {0.0,1.0,0.0};

float* cross(float* v1,float* v2)
{
    float el1[] = {(v1[1]*v2[2]-v1[2]*v2[1]),(v1[2]*v2[0]-v1[0]*v2[2]),(v1[0]*v2[1]-v1[1]*v2[0])};
    return el1;
}

float* neg3(float* v)
{
    float arr[3];
    for(int i=0;i<3;i++)
    arr[i] = 0.0-(v[i]);

    return arr;
}

/*

float* cam_space(float* p)
{
    float* e1 = cross(D,U);
    float* e2 = cross(D,cross(U,D));
    float* e3_n = D;

    float ar[4];
    ar[0] = e1[0]*p[0]+e1[1]*p[1]+e1[2]*p[2];
    ar[1] = e2[0]*p[0]+e2[1]*p[1]+e2[2]*p[2]; …
Run Code Online (Sandbox Code Playgroud)

c++ arrays pointers

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

联合会始终具有默认值为零吗?

请让我们考虑以下代码:

#include <iostream>
using namespace std;

union{
 int i;
}u;

int main(){

     int k=5;
     cout<<k+u.i<<endl;

    system("PAUSE");
    return EXIT_SUCCESS;
}
Run Code Online (Sandbox Code Playgroud)

该代码向我显示输出5,这对我来说意味着,联合结构中的变量i具有默认值= 0,但是ideone.com上的同一代码显示了这样的警告

prog.cpp:6: warning: non-local variable ‘<anonymous union> u’ uses anonymous type and then prints  5 as well, and last one  core of this problem comes  from algorithm calculate  
Run Code Online (Sandbox Code Playgroud)

平方根的倒数,这里是代码

#include<iostream>
#include<math.h>
using namespace std;

float invsqrt(float x){

    float xhalf=0.5f*x;

    union{
         float x;
         int i;
    }u;

   u.x=x;
   u.i=0x5f3759df-(u.i>>1);
   x=u.x*(1.5f-xhalf*u.x*u.x);

   return x;
}

int main(){

    float  x=234;
    cout<<invsqrt(x)<<endl;

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

它也向我显示了输出,但是我的问题是,这段代码是否好?我的意思是因为int我没有进行最后处理,所以任何编译器都可以认为它的值为零吗?我的问题不清楚我说的是我不是英语为母语的人。

c++

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

使用变量名打开文件

如果我将myfile("input.txt")更改为myfile(file_name)...其中file_name传递给函数它不起作用但是给错误没有匹配函数调用..我猜我不是bc我不是假设要将一个字符串传递给构造函数...如果不是这样的话......怎么样?

void file_to_string(string file_name)
{
   string line;  
   ifstream myfile("input.txt");
   if(myfile.is_open())
   {
      while(myfile.good())
      {
         getline(myfile,line);
         cout << line;
      }
      myfile.close();
  }
  else
  {
      cout << "File : " << file_name << " : did not open" ;
  }

}

int main(int argc, char *argv[])
{
    file_to_string(argv[1]);
}
Run Code Online (Sandbox Code Playgroud)

c++

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

长数组缓存问题

我最近注意到,使用循环扫描长数组会降低缓存性能.你能解释一下为什么会发生这种情况以及这个问题的解决方法.我正在使用C/C++开发Linux平台

c c++ performance memo

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

用于排序3个随机数的C程序,void reorder3(int a,int*b,int**c);

这段代码正在运行,但它并不完全是我想要的.有没有人知道如何使它正确,没有q排序?想法是了解如何使用指针.这三个数字应该是-3到12之间的随机数.下面的代码类似,我发现的最接近.任何帮助将非常感激.提前致谢!!.

#include <stdio.h>
#include <stdlib.h>

//functions
int compare(const void *a, const void *b)
{
   const int *ia = a;
   const int *ib = b;

   if (*ia < *ib)
      return -1;
   else if (*ia > *ib)
      return +1;

   return 0;
}

//qsort function
void sort3(int *a, int *b, int *c)
{
   int temp[3];

   temp[0] = *a;
   temp[1] = *b;
   temp[2] = *c;

   qsort(temp, 3, sizeof(int), &compare);

   *a = temp[0];
   *b = temp[1];
   *c = temp[2];
}

//random function
int rand_int(int a, …
Run Code Online (Sandbox Code Playgroud)

c sorting random pointers

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