I have a file which contains the following lines,
connection list
current check OK
connect "A" to "B"
connect "A" to "C"
connect "A" to "D"
connect "C" to "A"
connect "A" to "E"
Here connect "C" to "A" is a reverse connection to connect "A" to "C" The requirement is to remove the duplicate reverse connection.
I am new to C++ and vector. I tried using the following:
First I took a structure of 2 strings con1 and con2: connectPair Then I took a vector of the structure
Now, I am saving the file lines to a vector: rawFileLines I am trying to operate on rawFileLines to find the connection components.
I am storing the connection components to another vector: values
Here is my code:
typedef struct {
std::string con1;
std::string con2;
} ConnectPair;
void RemoveReversePairs(std::string inputFile) {
std::vector<std::string> fileData;
std::string line, scan, token1, token2;
std::size_t tokenLeft, tokenRight, maxLines, lineNumber = 0, pos = 0;
std::size_t found = 0, storeCount = 0;
std::vector<std::string> rawFileLines;
ConnectPair connectPair = {};
std::vector<ConnectPair> values;
std::ifstream source(inputFile.c_str());
while (std::getline(source, line)) {
rawFileLines.push_back(line);
}
source.close();
maxLines = rawFileLines.size();
for (size_t i = 0; i < maxLines; i++) {
line = rawFileLines[i];
pos = 0;
scan = "\"";
found = 0;
while (found < 2) /*line.find(scan, pos) != std::string::npos*/ {
tokenLeft = line.find(scan, pos);
tokenRight = line.find(scan, tokenLeft + 1);
if ((tokenLeft != std::string::npos) && (tokenRight != std::string::npos)) {
found++;
if (found == 1) {
connectPair.con1 = line.substr(tokenLeft + 1, (tokenRight - tokenLeft) - 1);
}
else if (found == 2) {
connectPair.con2 = line.substr(tokenLeft + 1, (tokenRight - tokenLeft) - 1);
values.push_back(connectPair);
storeCount++;
}
pos = tokenRight + 1;
}
else {
connectPair.con1 = "++";
connectPair.con2 = "++";
values.push_back(connectPair);
fileData.push_back(line);
break;
}
}
}
Now, I am having trouble comparing the connections. Please suggest me how to proceed.
Thank you.