Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 733525
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T07:16:02+00:00 2026-05-14T07:16:02+00:00

#include <list> #include <set> #include <iterator> #include <algorithm> using namespace std; class MyContainer {

  • 0
#include <list>
#include <set>
#include <iterator>
#include <algorithm>

using namespace std;

class MyContainer {
public:
 string value;

    MyContainer& operator=(const string& s) {
        this->value = s;
        return *this;
    }
};

int main() 
{
    list<string> strings;
    strings.push_back("0");
    strings.push_back("1");
    strings.push_back("2");

    set<MyContainer> containers;
    copy(strings.begin(), strings.end(), inserter(containers, containers.end()));
}

The preceeding code does not compile. In standard C++ fashion the error output is verbose and difficult to understand. The key part seems to be this…

/usr/include/c++/4.4/bits/stl_algobase.h:313: error: no match for ‘operator=’ in ‘__result.std::insert_iterator::operator* [with _Container = std::set, std::allocator >]() = __first.std::_List_iterator::operator* [with _Tp = std::basic_string, std::allocator >]()’

…which I interpet to mean that the assignment operator needed is not defined. I took a look at the source code for insert_iterator and noted that it has overloaded the assignment operator. The copy algorithm must uses the insert iterators overloaded assignment operator to do its work(?).

I guess that because my input iterator is on a container of strings and my output iterator is on a container of MyContainers that the overloaded insert_iterator assignment operator can no longer work.

This is my best guess, but I am probably wrong.

So, why exactly does this not work and how can I accomplish what I am trying to do?

  • 1 1 Answer
  • 2 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T07:16:03+00:00Added an answer on May 14, 2026 at 7:16 am

    What would work would be using the constructor (which would make more sense instead of the assignment):

    class MyContainer {
    public:
     string value;
    
        MyContainer(const string& s): value(s) {
        }
    };
    

    Then the second problem is that set also requires its contents to be comparable.

    As to the cause, insert_iterator works by overloading operator=:

    insert_iterator<Container>& operator= (typename Container::const_reference value);
    

    As you can see, the righthand value must be either the value type of the container or implicitly convertible to it, which is exactly what a (non-explicit) constructor achieves and the assignment operator doesn’t.


    Technically you could also make it work without changing the class (e.g if you don’t want an non-explicit constructor) by providing a suitable conversion function:

    MyContainer from_string(const std::string& s)
    {
        MyContainer m;
        m = s;    //or any other method how to turn a string into MyContainer
        return m;
    }
    

    which can be used with std::transform:

    transform(strings.begin(), strings.end(), inserter(containers, containers.end()), from_string);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i have following code #include <iostream> #include <set> #include <string> using namespace std; template<class
#include <list> using std::list; class foo { ... class bar : public foo {
here is my code for xor linked list implementation #include<iostream> using namespace std; struct
#include <list> template < class TYPE > class CIndex : protected std::list < TYPE
#include <list> using std::list; int main() { list <int> n; n.push_back(1); n.push_back(2); n.push_back(3); list
//file list.h #include stdafx.h namespace st { struct My_List; typedef My_List list; list* create(const
My classes look something like this (I include only the relevant properties): public class
Every example of list or set usage in Python seems to include trivial cases
I have an UITableView which includes a list of UITableViewCells. And I set the
Is it possible to automatically create Word documents which include list fields from a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.