使用 BUbble 排序C++进行矢量排序
Vector sorting using BUbble sort C++
我需要一个简单的程序来接受字符串向量并使用气泡排序进行排序并显示结果。同时执行程序自动终止。
#include<bits/stdc++.h>
#include<iostream>
#include<string.h>
#include<vector>
#include<algorithm>
using namespace std;
vector<string> v2;
void Bsortchar(vector <string> &ch)
{
int i, j;
string temp[1][200];
int charLength = ch.size();
cout<<ch.size();
for(i = 0; i <= charLength; i++)
{
for (j=0; j < (charLength -1); j++)
{
if (ch[j+1] < ch[j])
{
temp[1][200] = ch[j];
ch[j] = ch[j+1];
ch[j+1]= temp[1][200];
}
}
}
return;
}
int main()
{
int charSize;
//**********************************************************
cout<<"Enter the Size of the char Vector"<<endl;
cin>>charSize;
cout<<"Enter the char Vector"<<endl;
string c;
for(int i=0;i<=charSize;i++)
{
cout<<i<<":";
getline(cin,c);
v2.push_back(c);
}
//************************************************************
Bsortchar(v2);
//***********************************************************
cout<<endl<<"The sorted character vector array is : "<<endl;
for(int i=0;i<=charSize;i++)
{
cout<<v2[i]<<" ";
}
//***********************************************************
return 0;
}
需要接受来自用户的字符串,并在执行气泡排序后显示结果。
您有未定义的行为temp[1][200]
并且超出了这个(免费(二维数组的两个部分的末端。
这里不需要数组,只需要一个临时数组。
auto temp = ch[j];
ch[j] = ch[j+1];
ch[j+1]= temp;
或者,最好可以使用现有函数
std::swap(ch[j], ch[j+i]);
相关文章:
- 二叉排序树无法编译
- 仅使用绝对值对数组进行排序,并在C++中显示实际值
- C++选择排序算法中的逻辑错误
- 使用C++程序合并排序没有得到正确的输出
- 计算排序向量的向量中唯一值的计数
- 排序算法c++
- 使用2个键的cpp-stl::优先级队列排序不正确
- 将结构向量排序为子组
- 在c++中尝试对对象数组进行排序时,出现std:bad_alloc错误
- 如何对点云数据进行排序
- 对字符串进行排序时,在c++中处理sort()
- 是否有类似std::lower_bound的函数,而不需要排序/分区输入
- 下面是排序算法O(n)吗
- std::sort()函数无法对向量的一部分进行排序
- shell排序中的交换和比较
- clang格式:禁用排序包含
- 显示错误输出的简单数组排序程序
- 为什么我的排序算法会更改数组值
- 试图在c++中对数字列表进行排序
- 如何在C++中对数组进行冒泡排序