为什么我的插入排序函数不会更改我从 int main 传递的数组
Why my insertion sort function does not change the array that I passed from int main?
简短版本(使用硬连线输入)
#include <iostream>
#include <string>
#include <vector>
using namespace std;
void insertionSort(string theArray[], int n)
{
//unsorted = first index of the unsorted region,
//loc = index of insertion in the sorted region,
//nextItem = next item in the unsorted region,
//Initially, sorted region is theArray[0]
// unsorted region is theArray [1..n-1].
//In general, sorted region is theArray[0..unsorted-1],
// unsorted region theArray[unsorted.. n-1]
for(int unsorted = 1; unsorted< n; unsorted++)
{
//At this point, theArray[0..unsorted-1] is sorted.
//Find the right position (loc) in theArray[0..unsorted]
//for theArray[unsorted], which is the first entry in the
//unsorted region; shift, if necessary, to make room
string nextItem = theArray[unsorted];
int loc = unsorted;
while( (loc > 0) && (theArray[loc-1] > nextItem) )
{
//shift theArray [loc -1] to the right
theArray[loc] = theArray[loc-1];
}
// At this point, theArray[loc] is where nexItem belongs
theArray[loc] = nextItem; // Insert nextItem into sorted region
loc--;
}// end for
}
int main()
{
vector<string> token;
int countToken;
string input= "I,love,doing,nothing,at,all";
int count =0;
for(int i=0; i< input.length(); i++)
{
if(input[i] == ',')
count++;
}
countToken = count +1;
for(int i=0; i< countToken; i++)
{
int x= input.find(',');
token.push_back(input.substr(0,x));
input = input.substr(x+1);
}
cout << endl << "Current String: ";
for(int i =0; i< countToken; i++)
{
cout << token[i] <<" " ;
}
cout << endl;
string theArray[countToken];
for(int i =0; i< countToken; i++)
{
theArray[i] = token[i];
}
insertionSort(theArray, countToken);
cout << "SORTED: " ;
for(int i =0; i< countToken; i++)
{
cout << theArray[i] << " ";
}
return 0;
}// main
完整版
#include <iostream>
#include <string>
#include <vector>
using namespace std;
void insertionSort(string theArray[], int n)
{
//unsorted = first index of the unsorted region,
//loc = index of insertion in the sorted region,
//nextItem = next item in the unsorted region,
//Initially, sorted region is theArray[0]
// unsorted region is theArray [1..n-1].
//In general, sorted region is theArray[0..unsorted-1],
// unsorted region theArray[unsorted.. n-1]
for(int unsorted = 1; unsorted< n; unsorted++)
{
//At this point, theArray[0..unsorted-1] is sorted.
//Find the right position (loc) in theArray[0..unsorted]
//for theArray[unsorted], which is the first entry in the
//unsorted region; shift, if necessary, to make room
string nextItem = theArray[unsorted];
int loc = unsorted;
while( (loc > 0) && (theArray[loc-1] > nextItem) )
{
//shift theArray [loc -1] to the right
theArray[loc] = theArray[loc-1];
}
// At this point, theArray[loc] is where nexItem belongs
theArray[loc] = nextItem; // Insert nextItem into sorted region
loc--;
}// end for
}
int main()
{
vector<string> token;
int countToken;
while(1)
{
int answer;
cout << "MENU: " << endl;
cout << "1. Input String " << endl;
cout << "2. Sort " << endl;
cout << "3. Quit " << endl;
cout << endl;
cout << "Response: ";
cin >> answer;
cin.ignore(1000, 10);
if( answer == 1) // case 1. Input String
{
string input;
cout << endl << "Default delimiter is ','"<< endl << "Enter String: " ;
getline(cin, input);
// find delimiter
int count =0;
for(int i=0; i< input.length(); i++)
{
if(input[i] == ',')
count++;
}
//cout << "Count: "<< count <<endl;
//clear previous vector
token.clear();
countToken = count +1;
for(int i=0; i< countToken; i++)
{
int x= input.find(',');
token.push_back(input.substr(0,x));
input = input.substr(x+1);
}
//cout << "countToken: " << countToken << endl;
cout << endl << "Current String: ";
for(int i =0; i< countToken; i++)
{
cout << token[i] <<" " ;
}
cout << endl;
}
else if(answer == 2) // case 2. Sort
{
string theArray[countToken];
for(int i =0; i< countToken; i++)
{
theArray[i] = token[i];
}
//cout << "COUNTTOKEN: "<< countToken;
insertionSort(theArray, countToken);
cout << "SORTED: " ;
for(int i =0; i< countToken; i++)
{
cout << theArray[i] << " ";
}
}
else if(answer == 3)
{
break;
}
else
{
cout << endl << "Invalid input !" << endl << endl;
}
cout << endl;
}// while
return 0;
}// main
嗨,简要介绍一下我的程序。我要求用户输入一组带有","作为分隔符的字符串,以便在每次找到","时切断字符串,并将其推送到向量,然后将其传递给数组,然后将数组传递给插入排序函数。(注意:如果字符串中涉及任何空格,程序将无法正确剪切)
但是,我的插入排序函数似乎没有更改我正确传递的数组。当我调用函数时,我的程序甚至崩溃了。我读了一篇文章,它说数组默认通过引用传递 因此,我认为我的函数参数中的 & 符号 '&' 对于在 main 中更改我的数组是必要的。
我希望有人能弄清楚我的程序中缺少什么任何意见将不胜感激
谢谢
主要问题是您没有更改循环中loc
的状态。
while( (loc > 0) && (theArray[loc-1] > nextItem) )
{
//shift theArray [loc -1] to the right
theArray[loc] = theArray[loc-1];
//add this
loc--; // move to the left so that the shift can go on
}
相关文章:
- 我的 int main() 中出现堆栈溢出错误
- 在函数 'int main(int, char**) 中,没有声明 'MIN'
- 视频捕获与" int main(int argc, char **argv) "
- 'int main(int, int, std::__cxx11::string, std::__cxx11::string)'只需要零或两个参数 [-Wmain]
- C++ 从节点模拟 int main(int argc, char *argv[])
- 'int main(int argc, char* argv<::>)' 如何成为 main 的有效签名?
- C++ int main (int argc, char *argv[]) - 是 argv a c 样式数组
- 如何使用int main(int argc,char*argv[])运行c++proram
- 绕过 SDL2 中的"int main(int argc, char** argv)"
- int main(int, char const* const*) 格式良好
- int main(int argc,char* argv[]) 为什么 argc 给出 2 个参数
- OpenCV中int main(int argc,char**argv)中argc和argv的意义
- 将值传递给main(int,char**)
- 使用 int main(void) 编译失败;main(int argc, char *argv[]) successs
- int main(int argc, char **argv)
- GTEST_API_ int main(int argc, char **argv){}中的GTEST_API_是什么?
- 关于缺乏"int main(int argc, char* argv[])"知识
- 如何从终端启动C应用程序,并将参数传递给int main(int argc,char*argv[])
- 警告:'int main(int, char***)' 的第二个参数应该是 'char **' [-Wmain]
- main(int argc, char* argv[]) dont understand