Home > Back-end >  [C] use unordered_map <char, pair <int, int> problem
[C] use unordered_map <char, pair <int, int> problem

Time:10-17

 
Vector PartitionLabels (string STR) {
Unordered_map & lt; Char, pair The hash.
for(int i=0; iIf (hash. Find (STR) [I]==hash. The end ()) {
Hash. Insert ({STR [I] and {I, I}});
} else {
Hash [STR [I]]. Second=I;
}
}
Vector Ans.
for(int i=0; iInt clidx=hash [I]. Second. Second;

The last line error as follows, consult everybody bosses how to solve? I just want to remove the second value pair to assign a value to an int variable clidx,
 
Line 14: Char 39: error: member reference base type "int" is not a structure or union
Int clidx=hash [I]. Second. Second;
~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ^ ~ ~ ~ ~ ~ ~

CodePudding user response:

Unordered_map is not so traversal
Hash [] to return to the second element is the hash, namely pair, access to the second pair, get int, can't access the second int.

CodePudding user response:

You need to use this way to traverse the
 unordered_map & lt; Char, pair : : iterator iter. 
For (iter=hash. The begin (); Iter!=hash. The end (); Iter++) {
Int x=(* iter). Second. Second;

}

CodePudding user response:

If there is an inner loop when j j how to write?
 
For (iter=hash. The begin (); Iter!=hash. The end (); Iter++) {
Iter int clidx=(*). Second. Second;
For (auto j=iter + 1; J!=hash. The end (); J++) {
Int nfidx=hash * [j]. Second. First;

J that do such write right error is as follows:
 
Line 18: Char 30: error: invalid operands to binary expression (' unordered_map & lt; Char, pair : : iterator '(aka' _Node_iterator & lt; STD: : pair , __constant_iterators: : value, __hash_cached: : value> ') and 'int')
For (auto j=iter + 1; J!=hash. The end (); J++) {
~ ~ ~ ~ ^ ~

CodePudding user response:

reference amantuer reply: 3/f
if there is a inner circle of j j how to write?
 
For (iter=hash. The begin (); Iter!=hash. The end (); Iter++) {
Iter int clidx=(*). Second. Second;
For (auto j=iter + 1; J!=hash. The end (); J++) {
Int nfidx=hash * [j]. Second. First;

J that do such write right error is as follows:
 
Line 18: Char 30: error: invalid operands to binary expression (' unordered_map & lt; Char, pair : : iterator '(aka' _Node_iterator & lt; STD: : pair , __constant_iterators: : value, __hash_cached: : value> ') and 'int')
For (auto j=iter + 1; J!=hash. The end (); J++) {
~ ~ ~ ~ ^ ~

Why do you want? You'd better to learn the basic grammar well, hash the variables according to the way you write is wrong,
  • Related