0

Hey,
I have to write a program using vectors that will input a list of positive
integers terminated by a negative integer and output the unique numbers in the list in ascending order? Can anyone help me get started?

3
Contributors
4
Replies
5
Views
7 Years
Discussion Span
Last Post by Excizted
0

I will definitely need more help than that! What I actualy meant was do I need to convert them to integers and then sort or I can sort them without converting?

0

Well, you can use the string functions to find each number.. example with comma-separated numbers, spaces not allowed:

std::vector<int> v;
std::string numbers; // this is the string with your numbers
std::string::size_type pos = 0;// current position
while(pos != std::string::npos) // as long as current position is valid
{
    std::string::size_type start = pos; // temp storing pos in start
    pos = numbers.find_first_of(',', start); // setting pos to the next pos, which is comma
    v.push_back(atoi(numbers.substr(start, pos-start).c_str())); // cut from start to (next-comma-position minus current-position)
    pos++; // skip next comma, so we should be positioned at the next number
}

Then take a look at
http://www.cplusplus.com/reference/algorithm/sort/

Edited by Excizted: n/a

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.