本文介绍了在C ++中,如何将一个对象推送到一个向量,同时保持指向该对象的指针?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的代码中,我有一个Student对象的向量。

  m_students; 

我想要:


  1. 检查向量是否包含某个名字的任何学生。

  2. 如果没有这样的学生,请添加一个新的。

  3. 将数据添加到该姓名的学生。

请考虑以下代码:

  //检查学生是否已经存在。 
Student * targetStudent = NULL;
每个(学生在m_students)
{
if(student.Name()== strName)
{
targetStudent =& student;
break;
}
}

//如果学生不存在,添加它。
if(targetStudent == NULL)
{
targetStudent = new Student(strName);
m_students.push_back(* targetStudent);
}

//将课程信息添加到学生。
targetStudent-> Add(strQuarter,strCourse,strCredits,strGrade);当我调用 m_students.push_back(* targetStudent);
看起来向量m_students最后是一个targetStudent指向的Student对象的副本。



尝试添加到targetStudent不会更改向量中包含的对象。



我如何从指向对象的指针开始将该对象添加到向量中,然后访问在向量中的对象?

解决方案

STL容器复制它们包含的对象。没有办法解决这个问题。



但是,你可以有 std :: vector< std :: shared_ptr< Student> > ,它允许你有一个智能指针的容器。但是,为了这个工作,你的对象必须在构建时都附加到 shared_ptr



例如:

  std :: vector< std :: shared_ptr< Student> > m_students; 

std :: shared_ptr< Student> targetstudent;
每个(std :: shared_ptr< Student>学生在m_students)
{
if(student-> Name()== strName)
{
targetStudent =学生;
break;
}
}

//如果学生不存在,添加它。
if(!targetStudent)
{
//创建一个新学生并将其附加到智能指针
targetStudent.reset(new Student(strName));
m_students.push_back(targetStudent);
}

std :: shared_ptr 在C ++ 11中的< memory> 头中定义。 (在TR1中,你可以使用 std :: tr1 :: shared_ptr 。)如果你使用没有TR1的C ++ 98或者需要随身携带,你可以使用 boost :: shared_ptr 从下载。


In my code, I have a vector of Student objects.

vector<Student> m_students;

I want to:

  1. Check to see if the vector contains any Student of a certain name.
  2. If no such Student exists, add a new one.
  3. Add data to the Student of that name.

Consider the following code:

// Check to see if the Student already exists.
Student* targetStudent = NULL;
for each (Student student in m_students)
{
	if (student.Name() == strName)
	{
		targetStudent = &student;
		break;
	}
}

// If the Student didn't exist, add it.
if (targetStudent == NULL)
{
	targetStudent = new Student(strName);
	m_students.push_back(*targetStudent);
}

// Add the course info to the Student.
targetStudent->Add(strQuarter, strCourse, strCredits, strGrade);

When I make the call to m_students.push_back(*targetStudent); it seems that the vector "m_students" ends up with a copy of the Student object that "targetStudent" points to at that time.

The subsequent attempt to add to targetStudent does not change the object contained in the vector.

How can I, starting with a pointer to an object, add that object to a vector and then access the object that is in the vector?

解决方案

STL containers copy the objects they contain. There is no way to work around this.

You can, however, have a std::vector<std::shared_ptr<Student> >, which allow you to have a container of smart pointers. For this to work, though, your objects must all be attached to the shared_ptr at the time of construction.

So, something like:

std::vector<std::shared_ptr<Student> > m_students;

std::shared_ptr<Student> targetStudent;
for each (std::shared_ptr<Student> student in m_students)
{
        if (student->Name() == strName)
        {
                targetStudent = student;
                break;
        }
}

// If the Student didn't exist, add it.
if (!targetStudent)
{
        // creates a new Student and attaches it to smart pointer
        targetStudent.reset(new Student(strName));
        m_students.push_back(targetStudent);
}

std::shared_ptr is defined in the <memory> header in C++11. (In TR1, you can use std::tr1::shared_ptr instead.) If you're using C++98 without TR1, or need to be portable with it, you can use boost::shared_ptr instead; download from Boost.

这篇关于在C ++中,如何将一个对象推送到一个向量,同时保持指向该对象的指针?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-30 09:11