来自Vector的c ++ Reading Element在每个索引处打印相同的元素

时间:2015-12-14 04:15:36

标签: c++ vector

我有一个我在JAVA中实现的算法,但我想用MPI和OpenMP测试C / C ++中算法的性能。所以我正在编写代码。我多年没有接触过C ++,所以我需要一些建议和指导。我将类Task的对象放在一个向量中,并尝试使用for循环检查所有元素的值。

以下是我的Task.h文件的代码。我创建了tostring函数来打印对象的所有值。

   #include <vector>
   #include <string>

   //for std::vector
   #ifndef TASK_H
   #define  TASK_H


   class Task {
   public:
       Task();
       Task(const Task& orig);
       Task(int identity, float val, long st, long et, long ext, std::vector<int> deplist2, std::vector<int> pretask2);
       void setTId(int identity) ;
       int getTId() ;
       void setVal(float val) ;
       float getVal() ;
       long getStarttime() ;
       void setStarttime(long value) ;
       long getEndtime() ;
       void setEndtime(long value) ;
       long getExectime() ;
       void setExectime(long value) ;
       std::vector<int> getDeplist() ;
       void setDeplist(std::vector<int> value);
       std::vector<int> getPretask() ;
       void setPretask(std::vector<int> value);
       std::string toString();
       virtual ~Task();
   private:

   };

   #endif   /* TASK_H */

toString() Task.cpp的功能,下面是Task.cpp

   #include "Task.h"
   #include <vector>  //for std::vector
   #include <string>  //for std::string
   #include <iostream>
   #include <sstream>

   using namespace std;
   int id;
   float value;
   long starttime;
   long endtime;
   long exectime;
   std::vector<int> deplist;
   std::vector<int> pretask;

   void Task::setTId(int identity) {
    id = identity;
   }

   int Task::getTId() {
       return id;
   }

   void Task::setVal(float val) {
       value = val;
   }

   float Task::getVal() {
       return value;
   }

   long Task::getStarttime() {
    return starttime;
   }

   void Task::setStarttime(long value) {
    starttime = value;
   }

   long Task::getEndtime() {
    return endtime;
   }

   void Task::setEndtime(long value) {
    endtime = value;
   }

   long Task::getExectime() {
    return exectime;
   }

   void Task::setExectime(long value) {
    exectime = value;
   }

   std::vector<int> Task::getDeplist() {
    return deplist;
   }

   void Task::setDeplist(std::vector<int> value) {
       deplist = value;
   }

   std::vector<int> Task::getPretask() {
       return pretask;
   }

   void Task::setPretask(std::vector<int> value) {
       pretask = value;
   }

   string Task::toString() {
    std::ostringstream oss;

    int pSize = pretask.size();
    int depSize = deplist.size();
    string returnStr = "";
    cout << "printing " << id << endl;


    oss << "\n[ id = "<< id;

    oss << " , val = " << value << " startTime = " << starttime;

    oss << " , endTime = " << endtime;

oss << " , execTime = " << exectime;

oss << " depList = @/ ";
for (int var = 0; var < depSize; var++) {

    int tmp = deplist.at(var);
    oss << tmp << " , ";

}
oss << " @/ Pretasks = $<< ";

for (int var = 0; var < pSize; var++) {

    int tmp = pretask.at(var);
    oss << tmp << " , ";

    }

oss << " >>$ ] \n";
returnStr.append(oss.str());

return returnStr;
   }

   Task::Task() {

   }

   Task::Task(int identity, float val, long st, long et, long ext,
    std::vector<int> deplist2, std::vector<int> pretask2) {
id = identity;
value = val;
starttime = st;
endtime = et;
exectime = ext;
deplist = deplist2;
pretask = pretask2;
cout << "added task " << id<<endl;
   }

   Task::Task(const Task& orig) {

   }

   Task::~Task() {

   }

我的main函数创建了一个vector和inseert对象,如下面的

 std::vector<Task> taskList;
 taskList.reserve(num_tasks); // Set size of vector

    // populate the tasks
{

    int i;
    //#pragma omp parallel for private(i) shared (taskList) reduction(+:taskLength)
    for (i = 0; i < num_tasks; i++) {
        int rand = min + (std::rand() % (max - min + 1));
        taskLength+=rand;
        std::vector<int> deplist2;
        std::vector<int> pretask2;

        taskList.push_back(Task(i, rand, 0, 0, 0, deplist2,  pretask2));
        //taskList.insert(i,Task(i, rand, 0, 0, 0, deplist2,  pretask2));
                    cout <<i<<endl;
    }
}

{
    for (int var = 0; var < num_tasks; var++) {
        Task tmpT=taskList[var];
                    string prt= tmpT.toString();
                    cout << var << " "<<prt;
    }

}

但我的输出看起来像

  printing 9
  0 
  [ id = 9 , val = 12 startTime = 0 , endTime = 0 , execTime = 0 depList = @/  @/ Pretasks = $<<  >>$ ] 
  printing 9
  1 
  [ id = 9 , val = 12 startTime = 0 , endTime = 0 , execTime = 0 depList = @/  @/ Pretasks = $<<  >>$ ] 
  printing 9
  2 
  [ id = 9 , val = 12 startTime = 0 , endTime = 0 , execTime = 0 depList = @/  @/ Pretasks = $<<  >>$ ] 
  printing 9
  3 
  [ id = 9 , val = 12 startTime = 0 , endTime = 0 , execTime = 0 depList = @/  @/ Pretasks = $<<  >>$ ] 
  printing 9
  4 
  [ id = 9 , val = 12 startTime = 0 , endTime = 0 , execTime = 0 depList = @/  @/ Pretasks = $<<  >>$ ] 
  printing 9
  5 
  [ id = 9 , val = 12 startTime = 0 , endTime = 0 , execTime = 0 depList = @/  @/ Pretasks = $<<  >>$ ] 
  printing 9
  6 
  [ id = 9 , val = 12 startTime = 0 , endTime = 0 , execTime = 0 depList = @/  @/ Pretasks = $<<  >>$ ] 
  printing 9
  7 
  [ id = 9 , val = 12 startTime = 0 , endTime = 0 , execTime = 0 depList = @/  @/ Pretasks = $<<  >>$ ] 
  printing 9
  8 
  [ id = 9 , val = 12 startTime = 0 , endTime = 0 , execTime = 0 depList = @/  @/ Pretasks = $<<  >>$ ] 
  printing 9
  9 
  [ id = 9 , val = 12 startTime = 0 , endTime = 0 , execTime = 0 depList = @/  @/ Pretasks = $<<  >>$ ]

到目前为止,我已经尝试使用vector.at()方法来访问该函数,但它做了同样的事情。任何想法都会有帮助。谢谢:))

更新1:添加了Task.cpp

2 个答案:

答案 0 :(得分:0)

由于看起来打印出的元素每行都是插入的最后一个元素,因此您可能错误地创建了static个字段。如果您发布了Task课程的代码,将会很有帮助。具体来说,构造函数的实现,以及我假设您退出帖子的私有字段。另外,我想如果您努力将代码更改为一个完整的,最小的示例,您可能自己会发现错误。如果没有看到更多代码,很难说出导致问题的原因。

答案 1 :(得分:0)

发现Soluntion,问题是Task类的成员不是私有的。因此更改了我的头文件Task.cpp并添加了私人部分中的所有成员

#include <vector>
#include <string>

//for std::vector
#ifndef TASK_H
#define TASK_H

class Task {
public:
    Task();
//Task(const Task& orig);
    Task(int identity, float val, long st, long et, long ext,
        std::vector<int> deplist2, std::vector<int> pretask2);
    void setTId(int identity);
    int getTId();
    void setVal(float val);
    float getVal();
    long getStarttime();
    void setStarttime(long value);
    long getEndtime();
    void setEndtime(long value);
    long getExectime();
    void setExectime(long value);
    std::vector<int> getDeplist();
    void setDeplist(std::vector<int> value);
    std::vector<int> getPretask();
    void setPretask(std::vector<int> value);
    std::string toString();
    virtual ~Task();
private:
    int id;
    float value;
    long starttime;
    long endtime;
    long exectime;
    vector<int> deplist;
    vector<int> pretask;
};

    #endif  /* TASK_H */

这是我认为实际工作的事情(对不起还在学习)。 LOLz。使用

删除复制构造函数
Task(const Task& orig)