I hope to copy from container to another container.
But it is not easy. gdb said I'm trying to copy const string to string.
---------------------------------------------------------------------------------
0x00007ffff7b760f3 in std::string::assign(std::string const&) ()
from /usr/lib/libstdc++.so.6
---------------------------------------------------------------------------------
I don't know how to copy from container to container one by one.
WiIl you help me? thanks you.
This is my code.
---------------------------------------------------------------------

typedef class file_info 
{
public:	 
	int     ino_no;
	string  file_nm;
} file_inf;

vector<file_inf> input_stl; //Original container declaration

copy_123() {
vector<file_inf> inputA_stl; 
vector<file_inf> inputB_stl; 
	for(i = 1; i < input_stl.size(); i++) 
		{			
			j = i + 1;
			if(input_stl[i].ino_no != input_stl[j].ino_no)
				{
					inputA_stl[j].file_nm =input_stl[j].file_nm;
				       Acount++;
				}
            }
}

I hope to copy from container to another container.
But it is not easy. gdb said I'm trying to copy const string to string.
---------------------------------------------------------------------------------
0x00007ffff7b760f3 in std::string::assign(std::string const&) ()
from /usr/lib/libstdc++.so.6
---------------------------------------------------------------------------------
I don't know how to copy from container to container one by one.
WiIl you help me? thanks you.
This is my code.
---------------------------------------------------------------------

typedef class file_info 
{
public:	 
	int     ino_no;
	string  file_nm;
} file_inf;

vector<file_inf> input_stl; //Original container declaration

copy_123() {
vector<file_inf> inputA_stl; 
vector<file_inf> inputB_stl; 
	for(i = 1; i < input_stl.size(); i++) 
		{			
			j = i + 1;
			if(input_stl[i].ino_no != input_stl[j].ino_no)
				{
					inputA_stl[j].file_nm =input_stl[j].file_nm;
				       Acount++;
				}
            }
}

1. Your class is named file_info not file_inf
2. I don't understand why you are starting at index 1. C/C++ containers are indexed starting at 0
3. You are trying to set the jth value of a container that has nothing in it!

Carefully read this. You need to use push_back() to add values to a vector. However, you shouldn't be using them until you understand how they work. That goes for any construct in any programming language.

Use std::copy. If not then create your own like so.

#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
using namespace std;

template<typename Container>
void print(const Container c){
	Container::const_iterator itr = c.begin();
	cout << "{ ";
	while(itr != c.end()){
		cout << *itr++ << " ";
	}
	cout << "}"<<endl;
}
template<typename SrcIterator,typename DestIterator>
void myCopy(SrcIterator srcBegin, SrcIterator srcEnd, DestIterator dest){
	while(srcBegin != srcEnd){
		*dest++ = *srcBegin++;
	}
}
int main()
{	
	string src = "1234567890";
	std::vector<char> dest;
	
	//using std::copy method
	//copies src into dest, while expanding dest as needed
	copy(src.begin(),src.end(),std::back_insert_iterator<std::vector<char>>(dest));
	cout << "Vector now contains : ";
	print(dest);
	cout << endl;

	
	string src2 = "abcdefghijklmnopqrctuvwxyz";
	std::vector<char> dest2;

	//using myCopy function
	//copying src2 into dest2 while expanding size of dest2 if necessary
	myCopy(src2.begin(),src2.end(),std::back_insert_iterator<std::vector<char>>(dest2));
	cout << "Vector2 now contains : " ;
	print(dest2);
	return 0;
}

I recommend using std::copy so there is no chance for mistake.

commented: Thanks you so much. +1

I recommend using std::copy so there is no chance for mistake.

He's doing a conditional copy, so the std::copy won't work. He asked about a similar question in another thread and Duoas already suggested to use the remove_copy_if method. I don't think the OP is listening, though.

>> Use std::copy. If not then create your own.

Additionally a simple alternative is

vector<string> src;
  vector<string> dest; 
  
  src.push_back("abc");
  src.push_back("def");
  src.push_back("ghi");

  // make an exact copy of src, resizing dest
  dest = src;

He's doing a conditional copy, so the std::copy won't work. He asked about a similar question in another thread and Duoas already suggested to use the remove_copy_if method. I don't think the OP is listening, though.

But I'm still newbie.. He helped me .. Really I appreciate it.
But because of predicate part, I was not succeded.
Everybody programmed it at copy of number...
Thanks to him I tried to use copy_if, copy.. But not proper example to apply.
So sorry..
But really I appreciate everybody to help me..
Thanks you.

1. Your class is named file_info not file_inf
2. I don't understand why you are starting at index 1. C/C++ containers are indexed starting at 0
3. You are trying to set the jth value of a container that has nothing in it!

Carefully read this. You need to use push_back() to add values to a vector. However, you shouldn't be using them until you understand how they work. That goes for any construct in any programming language.

1. I think aliase name is file_inf. So it is ok.. no?
2. My original program contained copying 0th address.
2. I will try to copy ith value. Thanks you.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.