您好,这是我的代码:C++字符串转换为整数数组
#include<iostream>
#include <stdio.h>
#include <math.h>
void bubbleSort(int ar[]);
using namespace std;
int main()
{
char t = 'f';
char *t1;
char **t2;
cout<<t;
int choice;
std::cout << "\nWelcome to the algortihm tester!\n";
std::cout << "What algorithm would you like to test?";
std::cout << "\nChoose: \n1.Bubble Sort\n2.Selection Sort\n3.Insertion Sort\n";
scanf("\n%d", &choice);
switch(choice)
{
case 1:
std:: string trash;
std::string str;
std::cout << "\nINPUT:";
std::getline (std::cin,str);
std::getline(std::cin,trash);
int* myarray = new int[str.size() ];
std::copy(str.begin(), str.end(), myarray);
bubbleSort(myarray);
break;
}
}
void bubbleSort(int myarray[])
{
int length = sizeof(myarray)/sizeof(myarray[0]);
int i;
for(i=(length-1); i >= 0; i--)
{
for(int j =1; j<=i; j++)
{
if (myarray[j-1]>myarray[j])
{
int temp = myarray[j-1];
myarray[j-1]=myarray[j];
myarray[j]=temp;
}
}
}
}
我试图做的是从用户,一个字符串接受输入的程序,那么它就会被复制到一个数组,而数组传递给函数bubbleSort
。但是当我运行它,我得到的结果为0,这意味着,该字符串没有被正确地复制到数组。我是新来的c + +,并没有真正熟悉的语法,如何正确地将字符串转换为整数数组?
您的代码按原样被打破。 'main'函数和'switch'语句不完整。 – InternetAussie
我只拿这些,代码,因为我只是想评估一下情况1 –