小编Mik*_*our的帖子

STL迭代器进入构造函数

我想知道如何编写一个接受任何STL输入迭代器的自定义类(在本例中是一个链表)的构造函数.我已经创建了一个与我的List类绑定的自定义Iterator类.

这很好用.

template <typename T>  
List<T>::List(Iterator beg, Iterator end) : first_(0) {  
    while (beg != end)  
        insertLast(*beg++);
}
Run Code Online (Sandbox Code Playgroud)

我已经设法创建一个接收这样的列表迭代器的构造函数.

List<T>::List(typename list<T>::iterator s, typename list<T>::iterator e) :
    first_(0) {  
    while (s != e)   
        insertLast(*s++);
Run Code Online (Sandbox Code Playgroud)

我的STL-fu对于如何将其概括为接受任何输入迭代器并没有真正意义上的
任何帮助吗?

谢谢!

c++ constructor iterator stl

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

循环不工作

当我运行该程序时,它最终将其平均但它不会循环!有谁看到我做错了什么,并指出我正确的方向

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

//function prototypes 

void getTestScores(double &score1, double &score2, double &score3); 
double calcAverage(double &score1, double &score2, double &score3); 
void displayAverage(double avg);

int main() 
{   
    //declare variables
    double score1 = 0.0; 
    double score2 =0.0;     
    double score3 = 0.0; 
    double avg    = 0.0;

    //display average in fixed-point notation   
    getTestScores(score1,score2,score3);    
    calcAverage(avg);
    displayAverage(avg); 

    //enter scores
    while (avg != -1 ); 
    {
        cout << "score 1 (negative number to stop): ";
        cin >> score1;
        cout << "Enter score 2(negative …
Run Code Online (Sandbox Code Playgroud)

c++ visual-c++

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

如何在C中找到线指针?

如果我读取二进制文件,我可以使用以下方法找到第n行:

fseek(fp, 4*sizeof(line),SEEK_SET);
Run Code Online (Sandbox Code Playgroud)

但是在C中读取txt文件时,如:

1 1 2.2
2 3 3.001
3 4 5
Run Code Online (Sandbox Code Playgroud)

我无法确保一行的字节大小,因为在实际情况下,double值可以是2.2或3.0001或5.这次我怎样才能找到使用fseek的第n个?

谢谢!

c file-io file-upload file

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

多态地使用矢量多态与阵列之间的差异

基于更有效的C++ 第3项:永远不要多态地处理数组,我们应该避免多态地处理数组.

那么为什么我们可以使用std::vector指针指向基类而没有问题呢?

谢谢

#include <iostream>
#include "boost/shared_ptr.hpp"
#include <vector>

class BaseClass {
public:
    virtual void PrintMe() const {
        std::cout << "BaseClass" << std::endl;
    }
    virtual ~BaseClass() {}
};

class SubClass : public BaseClass {
public:
    virtual void PrintMe() const {
        std::cout << "SubClass" << std::endl;
    }
};

int main()
{
    std::vector<boost::shared_ptr<BaseClass> > vecPtrs;
    boost::shared_ptr<BaseClass> shp1(new BaseClass);
    vecPtrs.push_back(shp1);

    boost::shared_ptr<BaseClass> shp2(new SubClass);
    vecPtrs.push_back(shp2);

    for (size_t i = 0; i < vecPtrs.size(); ++i)
    {
        vecPtrs[i]->PrintMe();
    }
}

// Output: …
Run Code Online (Sandbox Code Playgroud)

c++ boost

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

c ++如何将double放入String中

那么在带有字符串的java中你可以这样做:

int stuff;
string otherstuff;

otherstuff = "I like this much stuff: " + stuff;
Run Code Online (Sandbox Code Playgroud)

但在C++中我不知道如何.

c++

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

使用十六进制值对 std::string 进行编码

我的应用程序需要在 std::string 中编码和传输一些十六进制值。所以我就这样做。

static string printHex(const string& str)
{
    stringstream ss;
    ss << "[ " << hex;
    for (int i = 0; i < str.size(); i++)
    {
        ss << (((uint32_t)str[i] )& 0xFF) << " ";
    }
    ss << "]" << dec;

    return ss.str();
}

int main()
{
    char ptr[] = {0xff, 0x00, 0x4d, 0xff, 0xdd};// <--see here, 0x00 is the issue.
    string str(ptr);
    cout << printHex(str) << endl;
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

显然,该字符串仅获取 0x00 以内的值,其余数据都会丢失。如果没有 0x00,它将适用于任何值。但我也需要 0x00。请提出一个解决方案。谢谢您的帮助。

c++

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

C++ 错误创建 n 个 pthreads-

在应该采用 int k 并创建 k 个 pthread 的函数中出现以下错误:

cse451.cpp:95:60: 错误:从 'void*' 到 'pthread_t* {aka long unsigned int*}' [-fpermissive] 的无效转换

cse451.cpp:97:54: 错误:无效使用无效表达式

我有一种感觉,它与 foo() 函数有关(此时仅用作占位符 foo(){} )

下面列出了相关代码(第 95 行是 pthread_t *threads......,第 97 行是 err=pthread_create....)

void createThreads(int k){
int numThreads = k;
int i = 0;
int err = 0;
pthread_t *threads = malloc(sizeof(pthread_t) * numThreads);
for(i = 0;i<numThreads;i++){
    err = pthread_create(&threads[i], NULL, foo(), NULL);
    if(err != 0){
        printf("error creating thread\n");
    }
}
}

void foo(){}
Run Code Online (Sandbox Code Playgroud)

c++ compiler-errors pthreads

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

c ++中是否有用于表示时间的类,以毫秒为单位?

c ++中是否有用于表示时间的类,以毫秒为单位?我需要保持时间并比较值,从设备设置时间.我需要自己写还是已经有?我看了看<ctime>,并time_t却能保持几秒钟.

c++

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

构造函数抛出异常的对象的异常处理是否可以接近基于堆栈的代码创建?

我正在尝试使我的C++代码异常安全,并且遇到一个问题,既不询问朋友也不搜索网络会有所帮助.

根据我的理解,当使用构造函数创建一个可能抛出异常的对象时,创建代码需要用try块括起来并且异常处理完成catch(){}.

如果创建是基于堆的(例如,new使用默认分配器编辑),我可以将异常处理代码放在创建附近,如下所示:

void f() {
  // work unrelated to Hoge object here

  try {
    Hoge *pHoge = new Hoge(); // could throw an exception
  } catch(HogeException& ex) {
    // handle exception
  }

  // rest of work here
}
Run Code Online (Sandbox Code Playgroud)

但是,如果创建是基于堆栈的,由于try块的范围,我无法找到方法来执行此操作并使用下面的代码:

void g() {
  // work unrelated to Hoge object here

  try {
    Hoge hoge; // could throw an exception

    // rest of work here
  } catch(HogeException& ex) {
    // handle exception
  } …
Run Code Online (Sandbox Code Playgroud)

c++ exception-handling

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

如何使用C++类列表

我会为这个问题疯狂.我正在尝试在c ++中使用list.我不能使用<vector><list>因为我的教授想要"纯粹的"c ++.(有了课,简而言之......).我可以创建只有一个int字段的列表,例如:

    class List{
    private:
        struct node{
            int *data;
            node* next;
        };  
        typedef struct node* nodePtr;
        nodePtr head;
        nodePtr curr;
        nodePtr temp;

    public: 
    List();
    void AddNode(int addData);
    void deleteNode(int delData);
    void PrintList();
};
Run Code Online (Sandbox Code Playgroud)

(这是有效的,这不是整个代码,但它有效.)

现在出现的问题:我怎样才能创建一个对象列表,而不是列出"int"数据?

例如,如果我创建人员列表,如地址簿,我该怎么办?

我会发疯的,请帮助我.提前致谢.

(抱歉我的英语不好,我不是那么好:)

c++ class linked-list list object

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