指向指针排序向量的指针向量的 C++ 向量
c++ vector of pointers to vector of pointers ordering
我有两个类,每个类都有一个指向Data
的指针向量。我想做的是将类向量中的指针分配给类Sample1
向量Sample2
中的指针。 问题是,当我在第二个向量中分配指针时,它们存储的顺序是第一个向量的顺序。我想按插入顺序存储它们。
下面是代码的最小可重现示例:
#include <iostream>
#include <vector>
using namespace std; //for sample purposes
// For simplicity, the data is just a string in this example.
using Data = string;
// In the real code there is a class with a certain vector as a member,
// but for this example we can reduce it to just the vector.
using Sample1 = vector<Data*>;
类样本 2— 问题就在这里
class Sample2 {
vector<Data*> autodromos2;
public:
vector<Data*>& getAutodromos() { return autodromos2; }
// ** This is the function with the problem. **
void addAutodromos2(vector<string>& arguments, vector<Data*>& autodromos)
{
for (Data* a : autodromos) {
for (string &s : arguments) {
if (s == *a) { // The real test is more complex.
getAutodromos().push_back(a);
break;
}
}
}
}
};
主函数(生成数据和调用addAutodromos2
(
int main()
{
// Create the list of elements to add to a `Sample2`.
// Note that these are strings, not Data objects (in the real code).
vector<string> arguments { "fourth", "first", "third" };
// Create the `Sample1` data with which to work.
Sample1 s1 {
new Data("first"), new Data("second"), new Data("third"),
new Data("fourth"), new Data("fifth")
};
// Create the `Sample2` data from the list and `s1`.
Sample2 s2;
s2.addAutodromos2(arguments, s1);
// Diagnostic:
for (Data* a : s2.getAutodromos()) {
cout << *a << endl;
}
}
输出为
first
third
fourth
什么时候应该
fourth
first
third
实际上循环的序列问题addAutodromos2()
您需要使用以下代码更改函数:
for (string s : arguments)
{
for (Data* a : autodromos)
{
if (s == *a) { // The real test is more complex.
getAutodromos().push_back(a);
break;
}
}
}
切换 for 环路。 输出fourth first third
希望这会有所帮助。
有一种思想流派认为,如果你在函数中嵌套循环,你可能不够抽象地思考。虽然这有时可能有点夸大其词,但在这种情况下确实有价值。让我们看一下内部循环。
for (string s : arguments) {
if (s == *a) {
getAutodromos().push_back(a);
break;
}
}
此循环在arguments
中搜索*a
,如果找到,则执行某些操作。搜索是一个可以抽象成自己的函数的概念,我们称之为found
,一个返回bool
的函数。
// Preliminary revision
void addAutodromos2(vector<string>& arguments, vector<Data*>& autodromos)
{
for (Data* a : autodromos) {
if ( found(arguments, *a) ) {
getAutodromos().push_back(a);
}
}
}
只需查看一个循环,应该更清楚问题是什么。元素将按照它们在autodromos
中的出现顺序添加到getAutodromos()
中。要使用arguments
内的顺序,您需要遍历它。(我将帮助程序函数的名称更改为find_by_name
,并让它将迭代器返回到找到的元素或结束迭代器。布尔返回值不再足够。
// Final revision
void addAutodromos2(vector<string>& arguments, vector<Data*>& autodromos)
{
for (string s : arguments) {
auto result = find_by_name(autodromos, s);
if ( result != autodromos.end() ) {
getAutodromos().push_back(*result);
}
}
}
这里缺少的是find_by_name
函数。好消息是,此任务非常常见,该功能是标准库的一部分,在标头<algorithm>
中。坏消息是使用库函数需要一些类型,因为参数更复杂(以获得更大的灵活性(。您可能需要定义一个包装器以使其专门针对您的案例。
// Returns an iterator to the element with the indicated name, or
// autodromos.end() if not found.
static auto find_by_name(const vector<Data*> & autodromos, const string & name)
{
return std::find_if(autodromos.begin(), autodromos.end(), [&name](Data *a){
return name == *a; // or name == a->get_name(), when Data is more complex
});
}
注意,如果真正的测试像比较name == *a
一样简单,那么可以使用std::find
代替std::find_if
,并且不需要使用lambda。 不要忘记在文件的前面#include <algorithm>
。
- 函数向量_指针有不同的原型,我可以构建一个吗
- 有没有一种"cleaner"的方法可以在指向基的指针向量中找到派生类的第一个实例?
- 使用向量类的c++指针
- 如何使用C++初始化向量;脚本化值不是数组、指针或矢量错误
- 如何更改唯一指针向量的可见性
- C++ 独特指针练习的向量
- C 中的向量指针的清晰图
- 当向量位于内部类中时,如何删除指向对象的向量指针
- std::向量指针
- 指向结构体的向量指针错误
- 删除指向指针的向量指针时出错
- 映射不同类型的向量指针
- 在c++中传递向量指针
- 虚拟类的向量:指针是干净的方法吗
- 如何从另一个向量/对象集构造新的向量/指针集
- 模型视图控制器- c++向量指针,太多的传递
- 为什么向量指针占用这么多内存?
- 删除条目向量指针的静态映射c++
- 复向量指针
- c++数组向量指针的初始化