Can you, please, help to get input of this format:
{1,2,3,4}
and convert it to array with integers?
int * ns = new int [n];
cin >> ns;
This does not work. How should I change it?
Can you, please, help to get input of this format:
{1,2,3,4}
and convert it to array with integers?
int * ns = new int [n];
cin >> ns;
This does not work. How should I change it?
using namespace std;
typedef istream_iterator<int> It;
vector<int> v;
copy(It(cin), It(), back_inserter(v));
You need to read the elements one by one and store them into the array.
int aNoOfElements = 0;
cin >> aNoOfElements;
int *anArray = new int[ aNoOfElements]; //allocate memory to hold aNoOfElements
for( int i = 0; i < aNoOfElements; i++ )
{
cin >> anArray[ i ]; // Read each input
}
You need to parse the input. Take the input as string, then check for the format conforming to what you want. An algorithm that you can use:
I hope you can turn above algorithm into working code, good luck :)
P.S.: Feel free to tell me if you spot a bug