| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 892 人关注过本帖
标题:compare vectors for equality
只看楼主 加入收藏
点线面
Rank: 8Rank: 8
来 自:NO.-1
等 级:蝙蝠侠
帖 子:525
专家分:980
注 册:2011-1-3
结帖率:100%
收藏
 问题点数:0 回复次数:1 
compare vectors for equality
Hello all
大家好
Could anyone please help?
有什么有人可以帮忙
I have got two structs.
我有两个结构体
 struct TRS_block
{
    std::string trsLineID;
    std::string id1;
    std::string id2;
   
    std::vector<std::string> raw_contents;
};
   
struct LC_block
{
    std::string lcLineID;
   
    std::vector<std::string> LC0413_IDs;
    std::vector<std::string> LC0419_IDs;
   
    std::vector<std::string> raw_contents;
};
 
Data in both structs has been stored in vectors. Both structs also contain vectors inside it.
两个数据结构体储存向量.两种结构也包含向量在其中。
 I want to match each element of the vector LC0413_IDs (of struct LC_block) with id1 and id2 (of struct TRS_block) for equality.
我想把每个元素的矢量LC0413_IDs LC_block)的id1结构和id2 TRS_block)结构的平等。
If any of the elements of vector LC0413_IDs matches either of the two ids i-e id1 and id2, then e-g a flag would be set to true.
如果一些元素的向量LC0413_IDs匹配两个i-e id1和id2其中之一,然后e-g的标志位为真
Could anyone please help, how do I go about it.
有什么人可以帮忙我实现它
Thanks
 谢谢
craniumonempty (148)     Jan 17, 2011 at 8:42pm
if you have vector<LC_block> LC_array; and just one element TRS_block TRS_var you could check like this (where i is declared beforehand and within LC_array... and j is declared and withing LC0413_IDs)
如果你有向量< LC_block > LC_array一种元素,只需要TRS_block这样你能不能核对一下TRS_var事先宣布在哪里,在LC_array…和j LC0413_IDs)在宣布,
 if ( LC_array[i].LC0413_IDs[j].compare(TRS_var.id1) == 0 ||
     LC_array[i].LC0413_IDs[j].compare(TRS_var.id2) == 0 )
{
  //.. set the flag
}

check http://www. for using compare...
 
craniumonempty (148)     Jan 17, 2011 at 8:45pm
TRS_var could be changed to vector<TRS_block> TRS_array and you would have another iterator to make sure is inside of TRS_array called k and change to
TRS_var就够了,TRS_array矢量< TRS_block >,你还会再迭代器,以确保内在的TRS_array叫k和改变
 if ( LC_array[i].LC0413_IDs[j].compare(TRS_array[k].id1) == 0 ||
     LC_array[i].LC0413_IDs[j].compare(TRS_array[k].id2) == 0 )
{
  //.. set the flag   //设置标志位
}
 
 Duoas (4716)     Jan 17, 2011 at 9:01pm
Overload the equality operator for them:
重载分配它们
 bool operator == ( const TRS_block& trs, const LC_block& lc )
  {
  return (trs.trsLineID    == lc.lcLineID)
     and (trs.id1          == lc.LC0413_IDs)
     and (trs.id2          == lc.LC0419_IDs)
     and (trs.raw_contents == lc.raw_contents);
  }

bool operator == ( const LC_block& lc, const TRS_block& trs )
  {
  return (trs == lc);
  }
 


Now you just need an overload for the vector content.
你现在只需要一个过载的向量的内容。
  #include <algorithm>
 
 bool operator == ( const std::vector <TRS_block> & trs, const std::vector <LC_block> & lc )
  {
  return (trs.size() == lc.size())
     and std::equal( trs.begin(), trs.end(), lc.begin() );
  }

bool operator == ( const std::vector <LC_block> & lc, const std::vector <TRS_block> & trs )
  {
  return (trs == lc);
  }
 


Now you can use it normally:

 vector <TRS_block> foo;
vector <LC_block> bar;

...

if (foo == bar) ...
 


Hope this helps.
 
GulHK (57)     Jan 17, 2011 at 10:14pm
Thanks craniumonempty and Thanks Duoas

Duoas the code gives me an error for all four overloaded operators one of which is as follows.
Duoas代码给了我一个是四重载操作符其中之一的错误。
error: ‘bool mergeTRSLCdata::operator==(const mergeTRSLCdata::TRS_block&, const mergeTRSLCdata::LC_block&)’ must take exactly one argument
 
wolfgang (110)     Jan 18, 2011 at 12:11am
That's because you're overloading them inside your structures. Overload them in the global scope.

 struct A
{
   // code
};

struct B
{
   //code
};

bool operator == (struct a, struct b)
{
   // compare code
}
 
 GulHK (57)     Jan 18, 2011 at 12:13am
wolfgang! I have initialized them in global space but it is still giving me this error.
 
wolfgang (110)     Jan 18, 2011 at 12:16am
From what the error says you have it inside mergeTRSLCdata:: because of the scope in front of the word operator. Remove it?
 
GulHK (57)     Jan 18, 2011 at 6:39pm
wolfgang I have done exactly the way Duoas suggested. I searched for the error on google and found that When overloading the == operator, define it as an external function, not as a member function, and declare that extern function a friend of the relevant class or struct. It removed the error. But now it gives me some other errors.

 friend bool operator == ( const TRS_block& trs, const LC_block& lc )
{                 
    return (trs.id1     == lc.LC0413_IDs)
          and (trs.id2  == lc.LC0419_IDs);
}

friend bool operator == ( const LC_block& lc, const TRS_block& trs )
{
    return (trs == lc);
}
      
friend bool operator == ( const std::vector <TRS_block> & trs, const std::vector <LC_block> & lc )
{
    return (trs.size() == lc.size())
         and std::equal( trs.begin(), trs.end(), lc.begin() );
}

friend bool operator == ( const std::vector <LC_block> & lc, const std::vector <TRS_block> & trs )
{
    return (trs == lc);
}
 



ERRORS:


 error: no match for ‘operator==’ in ‘trs->mergeTRSLCdata::TRS_block::id1 == lc->mergeTRSLCdata::LC_block::LC0413_IDs’
error: no match for ‘operator==’ in ‘trs->mergeTRSLCdata::TRS_block::id2 == lc->mergeTRSLCdata::LC_block::LC0419_IDs’
error: no match for ‘operator==’ in ‘trs == lc’
 



is it because the code is trying to compare a variable with a whole vector? Sorry for being silly as I dont know about the syntax. I want to compare each element of the vectors LC_block::LC0413_IDs and LC_block::LC0419_IDs with TRS_block::id1 and TRS_block::id2. If any of the elements of both these vectors equals any of the IDs then a flag should be set to true.

Please help.
 
jsmith (5013)     Jan 18, 2011 at 9:58pm
Yes. Lines 3 and 4 above are the problem. You are comparing a std::string against
a std::vector< std::string >, and no such operator== exists.

If you want to check if id1 exists in LC0413_IDs, you can do this:

  std::find( lc.LC0413_IDs.begin(), lc.LC0413_IDs.end(), trs.id1 ) != lc.LC0413_IDs.end()
 



If the above expression is true, then a match was found.

 
 
GulHK (57)     Jan 18, 2011 at 11:15pm
Thank you so much jsmith but I am still confused about the syntax ;'(

The vector that has struct TRS_block data is trs_file_as_blocks and the vector that has struct LC_block data is lc_file_as_blocks. How am I going to check if any of the elements of the two vectors of type struct LC_block is equal to either id1 or id2 of struct TRS_block. Could you please write to me the lines of code.
 
GulHK (57)     Jan 18, 2011 at 11:28pm
Should it be something like this?

 for ( itTRS = trs_file_as_blocks.begin() ; itTRS < trs_file_as_blocks.end(); itTRS++ )
    {
        for(std::vector<LC_block>::iterator itLC = lc_file_as_blocks.begin(); itLC != lc_file_as_blocks.end(); ++itLC)
        {   
            if(std::find( itLC->LC0413_IDs.begin(), itLC->LC0413_IDs.end(), itTRS->id1 ) != itLC->LC0413_IDs.end()
                || std::find( itLC->LC0413_IDs.begin(), itLC->LC0413_IDs.end(), itTRS->id2 ) != itLC->LC0413_IDs.end())
            {
               
            }
        }
    }
 



Could you please correct.
 
jsmith (5013)     Jan 19, 2011 at 12:15am
Let me understand. You have an instance of TRS_block and an instance of LC_block.
You want to check if id1 or id2 is present in either vector instead LC_block?

The line I gave you above, which you would put in your first operator== (lines 3 and 4)
checks if id1 is in one of the vectors. You have to write three more similar checks for
the other three permutations. (check if id1 is in the other vector, check if id2 is in the
first vector, check if id2 is in the second vector).
 
 
GulHK (57)     Jan 19, 2011 at 1:21am
You have an instance of TRS_block and an instance of LC_block. You want to check if id1 or id2 is present in either vector instead LC_block?

where those two vectors are declared inside the struct LC_block

The line I gave you above, which you would put in your first operator== (lines 3 and 4) checks if id1 is in one of the vectors.

I want to check if vector-1 of struct LC_block has an element equal to id1 of struct TRS_block or an element equal to id2 of struct TRS_block, similary check to see if vector-2 of struct LC_block has an element equal to id1 of struct TRS_block or an element equal to id2 of struct TRS_block.

 friend bool operator == ( const TRS_block& trs, const LC_block& lc )
{
    return (((std::find( lc.LC0413_IDs.begin(), lc.LC0413_IDs.end(), trs.id1 ) != lc.LC0413_IDs.end())
             || (std::find( lc.LC0413_IDs.begin(), lc.LC0413_IDs.end(), trs.id2 ) != lc.LC0413_IDs.end()))
             || ((std::find( lc.LC0419_IDs.begin(), lc.LC0419_IDs.end(), trs.id1 ) != lc.LC0419_IDs.end())
             || (std::find( lc.LC0419_IDs.begin(), lc.LC0419_IDs.end(), trs.id2 ) != lc.LC0419_IDs.end())));
}
 



That's the way I have done it without using the overloaded == operator and it is working fine but could you please modify the code for me according to the overloaded == operator?

 for ( std::vector<TRS_block>::iterator itTRS = trs_file_as_blocks.begin() ; itTRS < trs_file_as_blocks.end(); itTRS++ )
{
    for(std::vector<LC_block>::iterator itLC = lc_file_as_blocks.begin(); itLC != lc_file_as_blocks.end(); ++itLC)
    {   
        if((std::find( itLC->LC0413_IDs.begin(), itLC->LC0413_IDs.end(), itTRS->id1 ) != itLC->LC0413_IDs.end()
            || std::find( itLC->LC0413_IDs.begin(), itLC->LC0413_IDs.end(), itTRS->id2 ) != itLC->LC0413_IDs.end())
            || (std::find( itLC->LC0419_IDs.begin(), itLC->LC0419_IDs.end(), itTRS->id1 ) != itLC->LC0419_IDs.end()
            || std::find( itLC->LC0419_IDs.begin(), itLC->LC0419_IDs.end(), itTRS->id2 ) != itLC->LC0419_IDs.end()))
        {            
            // do something
        }
    }
}
 
 
 
jsmith (5013)     Jan 19, 2011 at 1:36am
Your first block of code does what you say you want it to do.

Your second block of code now says I have a vector of TRS_blocks and a vector of
LC_blocks, and I want to see if any element of the TRS_block vector is equal to
any element of the LC_block vector according to the rules set forth in the first
code block.

Your second block is basically what you need to do, though lines 5-8 are nothing
more than operator== which std::find will call:

 bool operator==( const std::vector<TRS_block>& trs, const std::vector<LC_block>& lc )
{
    typedef std::vector<TRS_block>::const_iterator TRS_iter;

    for( TRS_iter t = trs.begin(); t != trs.end(); ++t )
        if( std::find( lc.begin(), lc.end(), *t ) != lc.end() )
             return true;

    return false;
}
 
GulHK (57)     Jan 19, 2011 at 5:36pm
Thanks JSmith. But it still gives me an error.

error: no match for ‘operator==’ in ‘__first.__gnu_cxx::__normal_iterator<_Iterator, _Container>::operator* [with _Iterator = const mergeTRSLCdata::LC_block*, _Container = std::vector<mergeTRSLCdata::LC_block, std::allocator<mergeTRSLCdata::LC_block> >]() == __val’

I want to see if any element of the TRS_block vector is equal to any element of the LC_block vector according to the rules set forth in the first code block.

No I want to check if id1 OR id2 of the struct TRS_block is equal to any element of the two string type vectors contained in the struct LC_block

 struct TRS_block
{        
    std::string id1;
    std::string id2;
};
   
struct LC_block
{
    std::vector<std::string> LC0413_IDs;
    std::vector<std::string> LC0419_IDs;
};
 



Your code:

 friend bool operator == ( const std::vector<TRS_block>& trs, const std::vector<LC_block>& lc )
{
    typedef std::vector<TRS_block>::const_iterator TRS_iter;

    for( TRS_iter t = trs.begin(); t != trs.end(); ++t )
               if( std::find( lc.begin(), lc.end(), *t ) != lc.end() )
                   return true;
    return false;
}
 
Should it not be something like trs.id1 OR trs.id2 and lc.LC0413_IDs AND lc.LC0419_IDs and friend bool operator == ( const TRS_block& trs, const LC_block& lc ).

Please suggest.

Thanks
搜索更多相关主题的帖子: 结构体 
2011-01-19 18:21
快速回复:compare vectors for equality
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.028348 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved