您好,這是我的代碼: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 –