开发者

How do I insert a list of strings (text format) into a container alphabetically?

Supose i have

(A,B)
(A,C)
(A,D)
(B,C)
(B,D)
(C,D)
(D,E)

in a text file. I'll extract it using regular expressions.

I would like to insert the data in to a container so that it looks like this.

A->B,C,D
B->C,D
C->D
D->E

Which container do I use?

I need to be able to look up data on both the left hand and right hand side of the container, i.e. by key an value. So I need to be able to search/lookup

A,B,C,D in

A->B,C,D
B->C,D
C->D
D->E

and the B,C,D in

A->B,C,D

I need to be able to lookups and inserts in both the keys and the values to reiterate so if I get a (C,E) I can insert it to have

C->D,E
开发者_StackOverflow中文版


A std::vector with a one-off sort at the end may well be more efficient than something like a std::set (which will maintain ordering as you insert).

My advice would be to pick the one that best matches the semantics of what you want to do, and then modify it later if you find that it is inefficient.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜