0

I need to create a sort algorithm that takes an array called myArray where there is information from a txt file called start.txt. The problem is that I have inside the txt file many vectors like this: 2 2 30 30, where the first number is the robot number, the second the team, and the last two are the x and y position. I only need to order them in ascending order taking the robot number, but the output has to be the whole vector. Any ideas on how to do it?

#include "robot.h"
#include <iostream>
#include <fstream>
#include <string>
#include <algorithm>
#include <sstream>
#include <cstring>
#include <vector>
    
//class Robot {
//  std::string robotNum;
//  std::string robotTeam;
//  std::string robotPosX;
//  std::string robotPosY;
//  public:
//      Robot(std::string robotNum, std::string robotTeam, std::string robotPosX, std::string robotPosY) {
//          this->robotNum = robotNum;
//          this->robotTeam = robotTeam;
//          this->robotPosX = robotPosX;
//          this->robotPosY = robotPosY;
//      }       
    
//      std::string getRobotNum() {
//          return this->robotNum;
//      }
//      void setRobotNum(std::string robotNum) {
//          this->robotNum;
//      }
    
//};
//void bubbleSort(std::vector<Robot> myArray, int n) {
//int i, j;
//for (i= 0; i<n-1; i++)
    
//}
    
using namespace std;
string line;
std::vector<robot> myArray;
//string* sortArray(string* myArray) {
//bool sortByNum(const robot& lhs, const robot& rhs) { return lhs.robotNum < rhs.robotNum; }
    
    
//return myArray;
//}
void show() {
ifstream myfile;
myfile.open("start.txt");
//string line;
//std::vector<robot> myArray;
    
while (getline(myfile, line))
{
std::istringstream iss(line);
string line;
    
std::string segment;
std::vector<std::string> seglist;
    
while (std::getline(iss, segment, ' '))
{
seglist.push_back(segment);
}
    
//std::vector<robot> myArray;
            
robot robot(seglist[0], seglist[1], seglist[2], seglist[3]);
myArray.push_back(robot);
cout << seglist[0] << " " << seglist[1] << " " << seglist[2] << " " << seglist[3] << "\n";
            
            
//myArray[0].getRobotNum();
            
//for (robot &r : myArray)
//  std::cout << r.getRobotNum() << " _ " << std::endl;
    
//for (unsigned int i = 0; i <= unsigned(myArray.size()); i++) {
            
//}
    
cout << "\n";
}
    
//cout << myArray[];
myfile.close();
    
}
    
int main()
{
    
show();
    
        
return 0;
}
1
  • 4
    Use std::sort in algorithm header Commented Dec 14, 2020 at 10:04

1 Answer 1

2

Use std::sort with a custom comparator that only looks at the robot number

#include <algorithm>

std::vector<robot> myArray;
...
sort(myArray.begin(), myArray.end(), 
    [](const robot& x, const robot& y) { return x.getRobotNum() < y.getRobotNum(); }
);

The piece starting with [] is a lambda function that compares two robots using the robot number. This function is passed to std::sort which uses it to sort your vector.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.