Finding items that occur more than once in a vector


 
Thread Tools Search this Thread
Top Forums Programming Finding items that occur more than once in a vector
# 8  
Old 05-09-2011
Quote:
Originally Posted by LMHmedchem
I'm not sure how to create an empty map and add to it.
Code:
map<int,int> imap;

map[5]++;

This User Gave Thanks to Corona688 For This Post:
# 9  
Old 05-09-2011
Quote:
Originally Posted by Corona688
Code:
map<int,int> imap;

map[5]++;

Well that's embarrassingly simple. Smilie

Why am I always sticking my maps in an initialization function that returns the map?

Based on the above, can I do something simple like,
Code:
std::map<int,int> vec1Map;
myInt = 5;
vec1Map[myInt]++;
myInt = 2;
vec1Map[myInt]++;
myInt = 9;
vec1Map[myInt]++;
myInt = 5;
vec1Map[myInt]++;

to result in,
Code:
vec1Map[5,2];
vec1Map[2,1];
vec1Map[9,1];

or do I have to use something like find() to see if the key is already present?

LMHmedchem

---------- Post updated at 08:25 PM ---------- Previous update was at 07:20 PM ----------

This is what I have at the moment,
Code:
void loadMap(int lrdr, int k1, int k2, std::map<int,int> &getMap);

std::map<int,int> map1;

lrdr = 1; k1 = 17; k2 = 21;
loadMap(lrdr, k1, k2, map1);

void loadMap(int lrdr, int k1, int k2, std::map<int,int> &getMap) {
   int tmpInt;
   BondKey key(lrdr, k1, k2);
   BondMap::iterator iter = order_map.find(key);
   if (iter != order_map.end()) {
      BondInfo& bond_info = (*iter).second;
      for (int i=0; i<bond_info.m_nums.size(); i++) {
         tmpInt = ( (int) bond_info.m_nums[i].m_numI );
         getMap[tmpInt]++;
      }
   }
}

This retrieves some data from objects and gives me gives a map,
Code:
map1[7]  2
map1[14] 2
map1[21] 1
map1[22] 1

instead of the old vector, [7, 14, 21, 7, 14, 22], which is correct for the example I'm working on.

Now I need to parse the map back into two vectors, which I think I know how to do. Are there any bear traps in this so far? I tried to pass the map as a pointer, but the receiving function seemed to need to know the size of the map and it wouldn't compile.

LMHmedchem
# 10  
Old 05-10-2011
I guess, my previous example shows how to do it:
- it creates an empty map
- it fills the map step by step as the number are entered (until a negative number is given for stop).
- for the entered element, it adds 1 to the count.

Or am I missing something?

Loïc
This User Gave Thanks to Loic Domaigne For This Post:
# 11  
Old 05-10-2011
Quote:
Originally Posted by Loic Domaigne
I guess, my previous example shows how to do it:
- it creates an empty map
- it fills the map step by step as the number are entered (until a negative number is given for stop).
- for the entered element, it adds 1 to the count.

Or am I missing something?

Loïc
I was thinking that I would see separate assignments, like a 2D array. The ++list[x] just didn't catch my eye the right way when I was expecting something like list.insert(x,y);.

This is what I ended up with,
Code:
void loadMap(int lrdr, int k1, int k2, std::map<int,int> &buildMap);
std::map<int,int> map1;

lrdr = 1; k1 = 17; k2 = 21;
loadMap(lrdr, k1, k2, map1); // pass empty map to loading function

 std::vector<int> unique, nonUnique; // final vectors

// parse map into final vectors by count
for(std::map<int,int>::iterator it=map1.begin(); it!=map1.end(); ++it) {
   if (it->second == 1) {
      unique.push_back(it->first);
   } else {
      nonUnique.push_back(it->first);
   }
}

// loading function, most of this isn't relevant
void loadMap(int lrdr, int k1, int k2, std::map<int,int> &buildMap) {
   int tmpInt;
   BondKey key(lrdr, k1, k2);
   BondMap::iterator iter = order_map.find(key);
   if (iter != order_map.end()) {
      BondInfo& bond_info = (*iter).second;
      for (int i=0; i<bond_info.m_nums.size(); i++) {
         tmpInt = ( (int) bond_info.m_nums[i].m_numI );
         buildMap[tmpInt]++;
      }
   }
}

This gets me my unique and nonunique vectors and is quite a bit more efficient than my double loop. It also lets me easily handle doing different things for 2 occurrences, 3 occurrences, etc.

I guess I should be passing more or less everything but the map by const reference, since the map is the only thing that gets changed, so I should clean up a bit I think.

Thanks for all the help.

LMHmedchem

Last edited by LMHmedchem; 05-10-2011 at 02:04 PM..
# 12  
Old 05-10-2011
Quote:
Originally Posted by Loic Domaigne
I guess, my previous example shows how to do it:
- it creates an empty map
- it fills the map step by step as the number are entered (until a negative number is given for stop).
- for the entered element, it adds 1 to the count.

Or am I missing something?
I don't think you're missing anything. A map lets you just use the element like a vector and will add it as zero if missing, or just update it if already present.

You have to use iterators to check what's inside it however, because the mere act of checking if m[element] will also create that element as a zero!
# 13  
Old 05-10-2011
Quote:
Originally Posted by Corona688
You have to use iterators to check what's inside it however, because the mere act of checking if m[element] will also create that element as a zero!
That's interesting, the cpp reference page gives "operator[]" as element access. I wonder what they mean by that. I assume you can also use .find()?

LMHmedchem
# 14  
Old 05-12-2011
Quote:
Originally Posted by LMHmedchem
That's interesting, the cpp reference page gives "operator[]" as element access. I wonder what they mean by that.
It means what it says, you access elements with it, just like [] accesses elements in a vector. But unlike a vector, it's valid to access elements that don't exist yet! Do so and it'll just create it for you with default values(0 for atomic types). So if you did this the naive way on a map containing elements 0, 1, and 2:

Code:
for(n=0; n<1000; n++)
{
        if(mymap[n]) printf("Element %d exists %d times\n", mymap[n]);
}

...it'd create elements 3 through 999 for you and return their newly-created values of zero. Probably not what you want Smilie But an iterator will only give you elements that actually exist.
Quote:
I assume you can also use .find()?
Yes. I think map uses a list container internally. It's not a hash table.
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Cybersecurity

Where does Ciphering & Encryption occur?

Hello everyone. Upon reading the recent news about the NSA paying RSA to use a faulty cipher suite for it's default, it got me thinking... During a connection say for SSL, what is it that "knows" the rules for ciphers? Are these rules stored on the NIC? can they be edited, changed or appended? ... (3 Replies)
Discussion started by: Lost in Cyberia
3 Replies

2. Shell Programming and Scripting

Help with Perl script that can check a URL and notifiy when changes occur

I'm a scripting newbie and I'm trying to learn. No better way than being assigned a project. So basically, I'm trying to come up with a script that can periodically check a URL and then notify when changes occur to the file. So what I'm thinking is that I need to devise a PERL script that... (3 Replies)
Discussion started by: adam1mc
3 Replies

3. Shell Programming and Scripting

Print lines where variables occur more than once using grep

Hello, I want to only print lines where variables occur more than once using grep. For eg: Input: $this is a comment int a,b,c,b; int b,c; int d,e; int f,g,f; x=y+5; For the above input, the output would be int a,b,c,b; int f,g,f; I have done grep... (3 Replies)
Discussion started by: prasanna1157
3 Replies

4. UNIX for Dummies Questions & Answers

C shell loop problem occur

Hi all, i create 2 file Config path1 5 group1 path2 6 group2 path3 10 group1 path4 15 group2 Confine group1 andrew group2 alan In my C shell script i write like this: set line_array = (`cat $app_dir/config`) set line_array_2 =... (0 Replies)
Discussion started by: proghack
0 Replies

5. Shell Programming and Scripting

To find number of char occur

To find out number of "|" symbol is available in file: Input: a|b|c|d|z Ouput: 4 I am using below set of commands,It is working... Anybody have anyother solution using sed / awk. cnt=`wc -c <1.txt` cnt1=`tr -d "|" <1.txt >c.dat` cnt2=`wc -c <c.dat` outp=`expr $cnt... (19 Replies)
Discussion started by: Jairaj
19 Replies

6. Shell Programming and Scripting

finding missing items in file

I have a need to compare 2 files, then print results to file. Need to find items from file2 that are not found in file 1. thanks in advance! example: file 1: abcde=12 fffff=6 bbbb=35 file2: abcde=12 fffff=6 bbbb=35 ccccc=10 kkkkk=45 (8 Replies)
Discussion started by: discostu
8 Replies

7. Shell Programming and Scripting

awk between items including items

OS=HP-UX ksh The following works, except I want to include the <start> and <end> in the output. awk -F '<start>' 'BEGIN{RS="<end>"; OFS="\n"; ORS=""} {print $2} somefile.log' The following work in bash but not in ksh sed -n '/^<start>/,/^<end>/{/LABEL$/!p}' somefile.log (4 Replies)
Discussion started by: Ikon
4 Replies

8. Shell Programming and Scripting

how many times a word occur in afile

i want a shell script program for how many times a word occur in a file. i need not the line number but i want the counts of the particular word for eg:- hai how r u.. i am from andhra pradesh.. i am from tenali.i need this answer.i need it urgently.. i hope u will answer this ... ... (9 Replies)
Discussion started by: madhu.it
9 Replies

9. Shell Programming and Scripting

Why SIGKILL will occur?

Hi Gurus, I am executing my Datastage jobs on UNIX operating System. While running the jobs i am getting the following error: main_program: Unexpected termination by Unix signal 9(SIGKILL) Can any one please let me know what are the possible situations where this SIGKILL will arrise? ... (9 Replies)
Discussion started by: choppas
9 Replies
Login or Register to Ask a Question