Connecting Tech Pros Worldwide Help | Site Map

Modify some elements of vector

 
LinkBack Thread Tools Search this Thread
  #1  
Old June 17th, 2007, 10:35 AM
aarthi28@gmail.com
Guest
 
Posts: n/a
Default Modify some elements of vector

Hi,
I am trying to compare 2 vectors of different sizes, and if the same
element exists in both, I want to replace them by the text "aa". I
thought this would work, but it replaces the entire array with just
"aa". I'm not sure how else to do what I want to do


for (unsigned int i=0; i<a_word_list.size(); i++)
{
for (unsigned int j=0; j<n_word_list.size();j++)
{
if (a_word_list[i].compare( n_word_list[j]))
{
a_word_list.assign(1,"aa");

n_word_list.assign(1,"aa");

}
}
}


  #2  
Old June 17th, 2007, 11:35 AM
John Harrison
Guest
 
Posts: n/a
Default Re: Modify some elements of vector

aarthi28@gmail.com wrote:
Quote:
Hi,
I am trying to compare 2 vectors of different sizes, and if the same
element exists in both, I want to replace them by the text "aa". I
thought this would work, but it replaces the entire array with just
"aa". I'm not sure how else to do what I want to do
>
>
for (unsigned int i=0; i<a_word_list.size(); i++)
{
for (unsigned int j=0; j<n_word_list.size();j++)
{
if (a_word_list[i].compare( n_word_list[j]))
{
a_word_list.assign(1,"aa");
>
n_word_list.assign(1,"aa");
>
}
}
}
>
That's what assign does, it assigns the whole array. Also you are using
compare wrongly, compare returns 0 (i.e. false) if two items compare
equal. But you don't need compare, just use ==.

This looks more like it

for (unsigned int i=0; i<a_word_list.size(); i++)
{
bool replaced = false;
for (unsigned int j=0; j<n_word_list.size();j++)
{
if (a_word_list[i] == n_word_list[j])
{
n_word_list[j] = "aa";
replaced = true;
}
}
if (replaced)
n_word_list[i] = "aa";
}

I guess the trick is using the 'replaced' variable to track of if you've
made any changes to n_word_list, so at the end of the loop you know
whether to make the same change to a_word_list.

john
  #3  
Old June 17th, 2007, 11:45 AM
Robert Bauck Hamar
Guest
 
Posts: n/a
Default Re: Modify some elements of vector

aarthi28@gmail.com wrote:
Quote:
Hi,
I am trying to compare 2 vectors of different sizes, and if the same
element exists in both, I want to replace them by the text "aa". I
thought this would work, but it replaces the entire array with just
"aa". I'm not sure how else to do what I want to do
>
>
for (unsigned int i=0; i<a_word_list.size(); i++)
{
for (unsigned int j=0; j<n_word_list.size();j++)
{
if (a_word_list[i].compare( n_word_list[j]))
I'm assuming the wordlists holds elements of std::string? In that case you
should do:

if (a_word_list[i] == n_word_list[j])

The compare member function returns 0 if the strings are equal, a negative
number if the first is less than the second, and a positive value
otherwise. It's more or less the same as strcmp() in C.
Quote:
{
a_word_list.assign(1,"aa");
>
n_word_list.assign(1,"aa");
>
}
}
}
--
rbh
  #4  
Old June 17th, 2007, 11:45 AM
John Harrison
Guest
 
Posts: n/a
Default Re: Modify some elements of vector

>
Quote:
for (unsigned int i=0; i<a_word_list.size(); i++)
{
bool replaced = false;
for (unsigned int j=0; j<n_word_list.size();j++)
{
if (a_word_list[i] == n_word_list[j])
{
n_word_list[j] = "aa";
replaced = true;
}
}
if (replaced)
n_word_list[i] = "aa";
Typo, last line should be

a_word_list[i] = "aa";
  #5  
Old June 17th, 2007, 02:15 PM
James Kanze
Guest
 
Posts: n/a
Default Re: Modify some elements of vector

On Jun 17, 1:34 pm, John Harrison <john_androni...@hotmail.comwrote:
Quote:
aarth...@gmail.com wrote:
Quote:
Quote:
I am trying to compare 2 vectors of different sizes, and if the same
element exists in both, I want to replace them by the text "aa". I
thought this would work, but it replaces the entire array with just
"aa". I'm not sure how else to do what I want to do
Quote:
Quote:
for (unsigned int i=0; i<a_word_list.size(); i++)
{
for (unsigned int j=0; j<n_word_list.size();j++)
{
if (a_word_list[i].compare( n_word_list[j]))
{
a_word_list.assign(1,"aa");
>
Quote:
n_word_list.assign(1,"aa");
>
Quote:
}
}
}
Quote:
That's what assign does, it assigns the whole array. Also you are using
compare wrongly, compare returns 0 (i.e. false) if two items compare
equal. But you don't need compare, just use ==.
Especially since compare returns 0 (which converts to false) if
the two strings are equal:-).

--
James Kanze (Gabi Software) email: james.kanze@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 220,662 network members.