?
Solved

c++ std::mismatch problem with vector<string>

Posted on 2013-12-13
3
Medium Priority
?
1,464 Views
Last Modified: 2013-12-14
ref: std::mismatch
http://www.cplusplus.com/reference/algorithm/mismatch/?kw=mismatch

In this reference is sample code that uses std::vector<int>. When I modify the program to use std::vector<std::string>, the mismatch() returns two bad pointers in the pair. I added a std::equal() and it appears to work correctly. I also modified the sample program for  set_intersection to use std::vector<std::string>,  and it worked ok.
http://www.cplusplus.com/reference/algorithm/set_intersection/

How do you fix this program using std::mismatch? I am actually using std::set<std::string> so I don't need to sort it. (I would like to keep this in the non-C++11 domain as the OS is older and does not support this.)


// mismatch algorithm example
#include <iostream>     // std::cout
#include <algorithm>    // std::mismatch
#include <vector>       // std::vector
#include <string>
#include <utility>      // std::pair

bool mypredicate (std::string i, std::string j) {
  return (i==j);
}

int main () {

  bool isSame=false;
  std::string myStrs[] = {"10","20","80","320","1024"};   // myStrs: 10 20 80 320 1024
  std::vector<std::string> myvector(myStrs, myStrs + 5);
  std::vector<std::string> myvector1(myvector.begin(), myvector.end());

  // std::equal works, but std::mismatch returns bad pointers
  isSame = std::equal(myvector.begin(), myvector.end(), myvector1.begin());
  std::pair< std::vector<std::string>::iterator,std::vector<std::string>::iterator> mypair;
  mypair = std::mismatch (myvector.begin(), myvector.end(), myvector1.begin());
  std::cout << "First mismatching elements: " << *mypair.first;
  std::cout << " and " << *mypair.second << '\n';

  ++mypair.first; ++mypair.second;

  // using predicate comparison:
  mypair = std::mismatch (myvector.begin(), myvector.end(), myvector1.begin(), mypredicate);
  std::cout << "Second mismatching elements: " << *mypair.first;
  std::cout << " and " << *mypair.second << '\n';

  return 0;
}

Open in new window

0
Comment
Question by:phoffric
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
3 Comments
 
LVL 32

Author Comment

by:phoffric
ID: 39718244
Update: Although the intersection program worked with vector<string>, it did not compile with set<string>. But that can be another question. In case you are interested, the compiler error is on the std::set_intersection line. But when I make v (in line 12) a std::vector<std::string>, then it compiles, but gives an exception.
#include <iostream>     // std::cout
#include <algorithm>    // std::set_intersection, std::sort
#include <set>       // std::set
#include <vector>
#include <string>

int main () {
  std::string first[] = {"5","10","15","20","25"};
  std::string second[] = {"50","40","30","20","10"};
  std::set<std::string> f1(first, first+5);
  std::set<std::string> f2(second, second+5);
  std::set<std::string> v;
//  std::set<std::string>::iterator it;

  std::set_intersection (f1.begin(), f1.end(), f2.begin(), f2.end(), v.begin());

The compiler error is:
 error C2678: binary '=' : no operator found which takes a left-hand operand of type 'const std::basic_string<_Elem,_Traits,_Ax>' (or there is no acceptable conversion)

or

/usr/lib/gcc/i686-pc-cygwin/4.7.3/include/c++/bits/stl_algo.h: In instantiation of ‘_OIter std::set_intersection(_IIter1, _IIter1, _IIter2, _IIter2, _OIter) [with _IIter1 = std::_Rb_tree_const_iterator<std::basic_string<char> >; _IIter2 = std::_Rb_tree_const_iterator<std::basic_string<char> >; _OIter = std::_Rb_tree_const_iterator<std::basic_string<char> >]’:
set_intersection.cpp:17:79:   required from here
/usr/lib/gcc/i686-pc-cygwin/4.7.3/include/c++/bits/stl_algo.h:5914:6: error: passing ‘const std::basic_string<char>’ as ‘this’ argument of ‘std::basic_string<_CharT, _Traits, _Alloc>& std::basic_string<_CharT, _Traits, _Alloc>::operator=(const std::basic_string<_CharT, _Traits, _Alloc>&) [with _CharT = char; _Traits = std::char_traits<char>; _Alloc = std::allocator<char>; std::basic_string<_CharT, _Traits, _Alloc> = std::basic_string<char>]’ discards qualifiers [-fpermissive]

Open in new window

0
 
LVL 32

Author Comment

by:phoffric
ID: 39718291
UPDATE #2: Got intersection program in my previous post to work by making line 12 std::vector<std::string> v(10);
0
 
LVL 32

Accepted Solution

by:
phoffric earned 0 total points
ID: 39719118
I added this code to the OP and now it works with vector<string>
  if (mypair.first == myvector.end()) {
     std::cout << "mypair.first == myvector.end()" << std::endl;
     exit(0);
  }

Open in new window

I will try using set<string> to see if that works.

Update: After changing to set<string> and testing for the no-mismatch case, the program does not crash. The bad ptr that I referred to in the OP was actually myvector.end() which cannot be dereferenced.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.
Suggested Courses
Course of the Month13 days, 23 hours left to enroll

800 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question