Modify some elements of vector

A

aarthi28

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.compare( n_word_list[j]))
{
a_word_list.assign(1,"aa");

n_word_list.assign(1,"aa");

}
}
}
 
J

John Harrison

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.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 == n_word_list[j])
{
n_word_list[j] = "aa";
replaced = true;
}
}
if (replaced)
n_word_list = "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
 
R

Robert Bauck Hamar

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.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 == 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.
 
J

John Harrison

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 == n_word_list[j])
{
n_word_list[j] = "aa";
replaced = true;
}
}
if (replaced)
n_word_list = "aa";


Typo, last line should be

a_word_list = "aa";
 
J

James Kanze

(e-mail address removed) wrote:
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.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 ==.


Especially since compare returns 0 (which converts to false) if
the two strings are equal:).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,292
Messages
2,571,498
Members
48,185
Latest member
abhaysingh01

Latest Threads

Top