开发者

How to order a string[] in c++

The input is string[] like below.

"CSE111: CSE110 MATH101"

"CSE110:"

I need to order the strings based on some logic. For example my output should be a string[] like

"CSE110","MATH122","CSE111"

My question is

  1. While scanning through the input array, if one string is picked to be the first string of the output array, then how do I skip all occurrences of that particular string, while I continue to process the string[] for the second output string etc..

Eg:

Input:

"CSE111: CSE110 MATH101"

"CSE110:"

If CSE110 is picked to be the first string in the output, then when I scan through the input string[] for the second string to be a part of output, I should not consider CSE110.

How can I achieve this? The answer I am looking forward to is something like:

  1. Store the input in a string[]
  2. loop through the strings one by one using strtok or stringstream >> operator.
  3. Once the first string is found ...blah blah blah ....

Hope my question is clear enough. I will be glad to provide more details.

Edit1:More Explanation

The strings represent the order in which the classes need to taken . If a class has pre-requisite , the pre-requisite has to be t开发者_如何学运维aken first. ie. if Input is

"CSE111: CSE110 MATH101"
"CSE110:"

The class CSE111 has a pre-requisite of CSE110 MATH101 . So I need to consider first CSE1110(No Pre-requisite) - MATH101((No Pre-requisite) and then CSE111 . (Further Ties can broken in alphabetical order. ) I hope this helps..


I hopefully got it now: For a string of the form A: B C D, the course A has B, C, and D as prerequisites.

In that case you want a mapping from a course to its prerequisites, e.g.:

typedef std::set<std::string> CourseSet;
typedef std::map<std::string, CourseSet> Prerequisites;

Now you can fill a Prerequisites by tokenizing, using the first part as the key and a CourseSet as the value.

As it seems that you just want one of the possible orders for all courses in the input, you could then do the following:

  • complete the prerequisites for the courses (i.e. include the courses they indirectly depend on)
  • now a>b if a has b as a prerequisite
  • if b doesn't have a as a prerequisite use e.g. the lexicographical order
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜