如何在不使用插入排序更改原始向量的情况下对二维向量进行排序 我对C++和指针是新的,所以我只想问一下 如何在不直接排序的情况下对二维字符串向量x进行插入排序
最后它应该看起来像如何在不使用插入排序更改原始向量的情况下对二维向量进行排序 我对C++和指针是新的,所以我只想问一下 如何在不直接排序的情况下对二维字符串向量x进行插入排序,c++,pointers,vector,C++,Pointers,Vector,最后它应该看起来像 vector[vector[sorted]、vector[sorted]……] 要求:(不要直接对字符串进行排序,因为这会导致太多的数据移动。为了提高效率,请对指向字符串的指针进行排序。 )我唯一可以使用的库是iostream、vector和string 所以我必须创建一个指向2d向量的2d向量指针,然后对指针进行排序 所以我试着创造一个 vector<vector<string>> *p 看起来你只是把指针放错地方了-你不想要指向2D向量的指
vector[vector[sorted]、vector[sorted]……]
要求:(不要直接对字符串进行排序,因为这会导致太多的数据移动。为了提高效率,请对指向字符串的指针进行排序。
)我唯一可以使用的库是iostream、vector和string
所以我必须创建一个指向2d向量的2d向量指针,然后对指针进行排序
所以我试着创造一个
vector<vector<string>> *p
看起来你只是把指针放错地方了-你不想要指向2D向量的指针。您需要指向字符串的指针的2D向量,即:
std::vector
。我提出以下解决办法:
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
int main() {
// the original vector
std::vector<std::vector<std::string>> vec {
{"text", "some"},
{"multiple", "in", "vectors"},
{"to"},
{"sorted", "be"}
};
// the view - vector of vectors of pointers to the strings
std::vector<std::vector<const std::string*>> sorted_view{};
// for each vector of strings inside the vector of vectors...
for (const auto& v : vec) {
// ...we create a vector of views...
sorted_view.emplace_back();
// ...and push_back a pointer to that string into our view-vector
for (const auto& str : v) {
sorted_view.back().push_back(&str);
}
}
// for every view-vector...
for (auto& view : sorted_view) {
// ...sort the pointers of that vector according to what they point to
std::sort(view.begin(), view.end(), [](const auto& lhs, const auto& rhs) {
return *lhs < *rhs;
});
}
// print the view-vector
for (const auto& v : sorted_view) {
for (const auto ptr : v) {
std::cout << *ptr << ' ';
}
std::cout << '\n';
}
}
我相信这正是您想要的——查看分离的内部向量的排序数据。您认为为什么需要指针?您必须使用插入排序吗?如果没有,这将变得很简单:
for(auto&vec:shiftstring){std::sort(vec.begin(),vec.end());}
。记住要包含。或者,如果我误解了你,你能澄清一下“不改变x”是什么意思吗?顺便说一句,到位与否与此无关。你可以复制一份x
,然后应用上述解决方案……如果我们得到更多信息,我甚至可以为每个人提出(std::execution::par,…
@Fureeish我必须使用指针和插入排序这是必需的,我不能使用算法库唯一可以使用的库是向量字符串和iostream。所以问题是我不能修改移位字符串,我必须使用指针指向移位字符串,然后对指针的位置进行排序。所以当我找到指针的值。为什么必须使用指针?指针指向某些数据。如果不修改x
,则无法查看修改后的x
。请在问题描述中更具体一些。@Ted Lynmo下班后刚完成指针排序并做了一些测试,没有注意到任何“有效”的错误一个10000行文本文件的编译时间不同。是的,但我要注意我的分数,所以我要按她要求的去做,即使这很愚蠢。真的很感谢你的帮助help@daydrak我的荣幸。如果答案解决了你的问题,考虑一下点击它的分数就可以接受它。这将有助于未来的访问者SimIL的问题。ar的问题,以确保这确实是解决方案。刚刚做了,,灯泡出来后,我的头读了你的第一行。非常感谢
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
int main() {
// the original vector
std::vector<std::vector<std::string>> vec {
{"text", "some"},
{"multiple", "in", "vectors"},
{"to"},
{"sorted", "be"}
};
// the view - vector of vectors of pointers to the strings
std::vector<std::vector<const std::string*>> sorted_view{};
// for each vector of strings inside the vector of vectors...
for (const auto& v : vec) {
// ...we create a vector of views...
sorted_view.emplace_back();
// ...and push_back a pointer to that string into our view-vector
for (const auto& str : v) {
sorted_view.back().push_back(&str);
}
}
// for every view-vector...
for (auto& view : sorted_view) {
// ...sort the pointers of that vector according to what they point to
std::sort(view.begin(), view.end(), [](const auto& lhs, const auto& rhs) {
return *lhs < *rhs;
});
}
// print the view-vector
for (const auto& v : sorted_view) {
for (const auto ptr : v) {
std::cout << *ptr << ' ';
}
std::cout << '\n';
}
}
some text
in multiple vectors
to
be sorted