小编cpp*_*cpp的帖子

覆盖std的析构函数:异常

如果line 8被注释,以下程序不能在g ++ 4.4中编译.为什么?似乎当我覆盖std::exception构造函数时,我也必须覆盖它的析构函数.这是什么原因?

#include<iostream>
#include<exception>
using namespace std;

class A : public exception {
public:
    A(string msg) : _msg(msg) {}
    //~A() throw(){};                     // line 8
    const char* what() const throw() { return _msg.c_str();}    
private:
    string _msg;
};

int main()
{
}
Run Code Online (Sandbox Code Playgroud)

编译错误是:

error: looser throw specifier for ‘virtual A::~A()’
Run Code Online (Sandbox Code Playgroud)

c++ destructor exception-handling exception throw

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

文件和控制台流之间的区别

如何确定天气ostream是文件或控制台流.在下面的程序中我想打印"Hello file!" 写入文件和"Hello console!"时 在写入控制台时.我应该在第17行指定什么条件?

#include <fstream>
#include<iostream>
#include <string>
using namespace std;

class A{
public:
        A(string msg):_str(msg){}
        string str()const {return _str;};
private:
        string _str;
};

ostream & operator << (ostream & os, const A & a)
{
        if (os is ofstream) //this is line 17
                os << "Hello file! " << a.str() << endl;
        else
                os << "Hello console! " << a.str() << endl;

        return os;
}

int main()
{
        A a("message");
        ofstream ofile("test.txt");
        if (!ofile)
                cerr << "Unable …
Run Code Online (Sandbox Code Playgroud)

c++ fstream ostream

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

C++中数组的每个n元素

是否有更有效的方法来获得数组的每一秒(一般每N个)元素,然后是下面的简单for循环?例如,使用通用算法?

#include<iostream>
using namespace std;

int main()
{
    const int a_size = 6, b_size = 3;
    int a[a_size] = {1, 3, 6, 3, 2, 7}; 
    int b[b_size];
    int bx = 0;
    for ( int ax = 0; ax < a_size; ++ax )
    {   
        if (ax % 2 == 0)
            b[bx++] = a[ax];  
    }   
}
Run Code Online (Sandbox Code Playgroud)

c++

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

究竟在.bashrc中设置了PATH?

在.bashrc文件的末尾,我添加了这些行来设置foo我的主目录中文件夹的路径:

PATH = $PATH:/home/username/foo
export PATH;
Run Code Online (Sandbox Code Playgroud)

然后我输入bash:

source .bashrc
Run Code Online (Sandbox Code Playgroud)

这些产生的错误:

bash: PATH: command not found
Run Code Online (Sandbox Code Playgroud)

我正在使用Debian Squeeze.在这里的类似问题中,建议修改/etc/login.defs.我不想这样做,因为它写的是login.defs:

add the rest [of your paths] in the shell startup files
Run Code Online (Sandbox Code Playgroud)

如何foo在.bashrc 中将文件夹添加到PATH?

linux debian path

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

在switch语句中使用向量时出现"跳转到大小写标签"错误.

这是代码,当我添加其他案例或deafult时,我得到了几个错误.我找不到任何基本错误,如缺少分号左右,当我只有一个案例时代码正常工作.我通过切换教程搜索,但我没有发现任何关于向量和切换语句混合是一个问题.

int main()
{
int r; 
while (cin >> r)
{
    switch (r) 
    {
       case 3:
    int y = 0;
    cout << "Please enter some numbers, and I will put them in order." << endl; 
    vector<int> nums;
    int x;
    while(cin >> x)
    {
        nums.push_back(x);
        y++;
    }
    sort(nums.begin(), nums.end());
    int z = 0;
    while(z < y)
    {
        cout << nums[z] << ", ";
        z++;
        if(z > 23)
            cout << "\n" << "User... What r u doin... User... STAHP!" << endl;
    } …
Run Code Online (Sandbox Code Playgroud)

c++ vector switch-statement

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

是否可以使用matlab访问数组,如C++中的索引?

我正在考虑使用类似索引的matlab访问C++数组,例如a[1 , :].这将非常方便.

我重载了operator [],但似乎不可能有a[1, : ]语法.

我想出的解决方案是编写一个脚本,该脚本预处理C++源代码并a[1, :]转换为C++函数,例如col(a, 1).但这似乎很费力.

有没有人有更好的解决方案?谢谢!!

c++

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

odeint simple 1d ode示例无法编译

我尝试在Debian Squeeze g ++ 4.4上的boost_1_54_0中运行odeint示例

Lorenz系统工作正常,但简单的1d ode:

#include <iostream>
#include <boost/numeric/odeint.hpp>


using namespace std;
using namespace boost::numeric::odeint;


/* we solve the simple ODE x' = 3/(2t^2) + x/(2t)
 * with initial condition x(1) = 0.
 * Analytic solution is x(t) = sqrt(t) - 1/t
 */

void rhs( const double x , double &dxdt , const double t )
{
    dxdt = 3.0/(2.0*t*t) + x/(2.0*t);
}

void write_cout( const double &x , const double t …
Run Code Online (Sandbox Code Playgroud)

c++ boost odeint

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

带字符串顶点的Boost图

我知道如何在Boost Graph中创建具有整数或char顶点的图形(请参见下面的注释代码)。问题是如何重写此代码以使用字符串顶点?

#include <string>
#include <boost/graph/adjacency_list.hpp>

using namespace boost;

int main (int argc, char **argv)
{
        typedef adjacency_list <vecS, vecS, undirectedS> vector_graph_t;

        //works
        //typedef std::pair <int, int> E;
        //E edges[] = { E (2, 5), E (5, 3), E (3, 1), E (5, 1)};
        //vector_graph_t g (&edges[0],&edges[0] + sizeof(edges) / sizeof(E), 4);

        //works
        //typedef std::pair <char, char> E;
        //E edges[] = { E ('a', 'b'), E ('a', 'c'), E ('x', 'a'), E ('b', 'x')};
        //vector_graph_t g (&edges[0],&edges[0] + sizeof(edges) …
Run Code Online (Sandbox Code Playgroud)

c++ boost graph boost-graph

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

如何从另一个会话中终止后台任务?

我在后台运行了一个多线程程序:

./my_task &
Run Code Online (Sandbox Code Playgroud)

然后我退出,然后再次登录。现在jobs命令不显示该程序,但top显示该程序的线程。所以它仍然在运行。怎么阻止呢?我想我可以杀死每个线程,但是线程很多,我不知道它会如何影响my_task程序。

我正在使用 Debian Squeeze。

linux bash background-process

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

以相反的顺序打印矢量

是否有更好的方法以相反的顺序打印矢量,然后:

#include<vector>
#include<iostream>
#include<algorithm>
using namespace std;

void print_elem(int elem)
{
    cout << elem << endl;    
}

int main()
{
    int ia[4]={1,2,3,4};
    vector<int> vec(ia,ia+4);
    reverse(vec.begin(), vec.end());
    for_each(vec.begin(),vec.end(),print_elem);
    reverse(vec.begin(), vec.end());
}
Run Code Online (Sandbox Code Playgroud)

c++ stl vector

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