C++ 如何实现对向量元素的顺序添加,在插入之前对元素进行排序?
我试图编写一个程序,将元素插入向量,按字母顺序排序。插入之前的元素与另一个元素进行比较,直到超过已插入的元素。假设使用C++ 如何实现对向量元素的顺序添加,在插入之前对元素进行排序?,c++,string,algorithm,vector,stl,C++,String,Algorithm,Vector,Stl,我试图编写一个程序,将元素插入向量,按字母顺序排序。插入之前的元素与另一个元素进行比较,直到超过已插入的元素。假设使用.insert()添加比较元素之后。我想在不使用排序算法的情况下实现它 std::string name; std::vector<std::string> students; std::vector<std::string>::iterator beg = students.begin(); while (std::cin>>name){
.insert()
添加比较元素之后。我想在不使用排序算法的情况下实现它
std::string name;
std::vector<std::string> students;
std::vector<std::string>::iterator beg = students.begin();
while (std::cin>>name){
for (std::vector<std::string>::iterator e = students.end() ; beg !=e ; ) {
if (!name.compare(*beg))
{
students.insert(beg, name);
break;
}
else
beg++;
}
}
std::字符串名称;
性病:病媒学生;
std::vector::iterator beg=students.begin();
while(std::cin>>名称){
for(std::vector::iterator e=students.end();beg!=e;){
如果(!name.compare(*beg))
{
学生。插入(beg,姓名);
打破
}
其他的
beg++;
}
}
为了避免指向最后一个元素的迭代器失效,我在每次迭代中更新它。
问题是,在这部分代码之后,我检查了向量,但它是空的。对于空向量,开始和结束是相同的,因此从不插入任何内容 不清楚您为什么不想使用排序算法,因此我建议如下:
std::string name;
std::vector<std::string> students;
while (std::cin>>name){
students.push_back(name);
}
std::sort(students.begin(),students.end());
std::字符串名称;
性病:病媒学生;
while(std::cin>>名称){
学生们。推回(姓名);
}
std::sort(students.begin(),students.end());
或者,用您最喜欢的排序例程替换最后一行。此比较
if (!name.compare(*beg))
没有道理。它只检查两个字符串是否相等
例如,考虑以下代码片段
std::string s1 = "one";
std::string s2 = "one";
std::cout << !s1.compare( s2 ) << '\n';
必须在外部while循环内。也就是说,迭代器应在循环的每次迭代中重新初始化
下面是一个演示程序,演示如何实现内部循环
#include <iostream>
#include <string>
#include <vector>
#include <iterator>
void insert( std::vector<std::string> &v, const std::string &s )
{
auto it = std::begin( v );
while ( it != std::end( v ) && not( s < *it ) ) ++it;
v.insert( it, s );
}
int main()
{
std::string names[] = { "One", "Two", "Three" };
std::vector<std::string> v;
for ( const auto &s : names )
{
insert( v, s );
}
for ( const auto &s : v ) std::cout << s << ' ';
std::cout << '\n';
return 0;
}
也就是说,字符串按升序插入
相对于您的代码片段,循环可以是这样的
while ( std::cin >> name )
{
auto it = std::begin( students ); // or students.begin()
while ( it != std::end( students ) && not( name < *it ) ) ++it;
students.insert( it, name );
}
显示的代码显然不是“每次迭代都更新”,尽管您声称。所示代码的哪一部分在每次迭代时正确重置
beg
迭代器?未定义的行为。事实上,我会更改它,compare()
不是布尔函数,它的工作方式与您认为的不同。你的比较是错误的。对措辞吹毛求疵:根据定义,不使用算法就无法排序。任何将向量排序的代码都是算法的实现。我猜你是说
中没有标准算法。但是为什么呢?还要注意,每次插入后的排序不是很有效。那么最好接受所有的意见sort@Konstantin您已将其标记为算法
,因此,您应该使用std::lower\u bound
或std::upper\u bound
。向量
已经排序,因此通过二进制搜索项目应该去的地方可以更有效地将项目插入到排序向量中,而不是从头开始并比较每个元素。这是真正完整的答案)但是我们如何在这部分代码中使用std::end(v)
(std::vector&v,const std::string&s){auto it=std::begin(v);while(it!=std::end(v)&¬(s<*it))++it;v.insert(it,s);}
如果我们每次迭代都使用“结束”迭代器,我一定不明白something@Konstantin从C++ 11开始,一般函数有STD::开始和STD::在报头中声明的端,允许将它们应用到任何标准容器或数组。
end迭代器已更改,不是吗?但在下一次迭代中,您使用了与函数相同的end
@Konstantin,然后使用该函数重新计算end迭代器。
One Three Two
while ( std::cin >> name )
{
auto it = std::begin( students ); // or students.begin()
while ( it != std::end( students ) && not( name < *it ) ) ++it;
students.insert( it, name );
}
#include <iostream>
#include <string>
#include <functional>
#include <vector>
#include <iterator>
#include <algorithm>
//...
while ( std::cin >> name )
{
using namespace std::placeholders;
auto it = std::find_if( std::begin( students ), std::end( students ),
std::bind( std::greater_equal<>(), _1, name ) );
students.insert( it, name );
}