Vector Erase and C++ boost paths

Hi Guys,

I'm pretty new moving from C to C++ so please excuse me if this has an obvious answer... any help will be much appreciated.

I am using the boost filesystem library.

I have created the following function where I am reading in directory contents. (I think the split between dir and file is useless here but as my code doesn't yet work I'm leaving that as a detail for now)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
typedef std::vector<path> PathSet_t;

bool ReadInDirContentsAsRelativeNames(path const &p, PathSet_t &fileSet, PathSet_t &subDirSet)
{
	directory_iterator end_itr;

	if( exists(p) && is_directory(p) )
	{
		for( 
			directory_iterator it = directory_iterator(p); 
			it != end_itr; 
			++it )
		{
			path currPath = (*it).path();
			
			if( is_directory(currPath) )
			{
				subDirSet.push_back(currPath.filename());
			}
			else if( is_regular_file(currPath) )
			{
				fileSet.push_back(currPath.filename());
			}						
		}

		sort(subDirSet.begin(), subDirSet.end());
		sort(fileSet.begin(), fileSet.end());

		return true;
	}

	return false;
}


Then, what I am doing is comparing the contents of two directories A and B. I want to have 3 lists: files common to both A and B, files only in A, and files only in B. To do this I am using the following code snippet...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
...snip...
/* Get the contents of both the vendor-main and vendor-drop directories
	* sorted into two sets, one for files an one for sub directories */
ReadInDirContentsAsRelativeNames(vendorMainRootDir, vendorMainFileSet, vendorMainDirSet);
ReadInDirContentsAsRelativeNames(vendorDropRootDir, vendorDropFileSet, vendorDropDirSet);

/* Get the sub dirs that are common to both the current directories in 
	* the vendor-main and vendor-drop trees */
set_intersection(
	vendorMainDirSet.begin(), vendorMainDirSet.end(), 
	vendorDropDirSet.begin(), vendorDropDirSet.end(), 
	std::insert_iterator<PathSet_t>(commonDirSet, commonDirSet.begin()) );

/* Get the files that are common to both the current directories in 
	* the vendor-main and vendor-drop trees */
set_intersection(
	vendorMainFileSet.begin(), vendorMainFileSet.end(), 
	vendorDropFileSet.begin(), vendorDropFileSet.end(), 
	std::insert_iterator<PathSet_t>(commonFileSet, commonFileSet.begin()) );


for(PathSet_t::const_iterator it = commonFileSet.begin(); it != commonFileSet.end(); ++it)
{
	PathSet_t::iterator elem = find(vendorMainFileSet.begin(),vendorMainFileSet.end(), *it);
	if( elem != vendorMainFileSet.end() )
	{
		vendorMainFileSet.erase(it);
	}

	elem = find(vendorDropFileSet.begin(),vendorDropFileSet.end(), *it);
	if( elem != vendorDropFileSet.end() )
	{
		vendorDropFileSet.erase(it);
	}

...snip...
}


The vendorMainFileSet.erase(it) is causing an Access Violation. I'm not sure why. I think I am putting new paths into the vector using the copy constructor so when erased the element shouldn't complain... is that right? Perhaps some memory is being shared between the paths?

If you have any clues Id love to hear them....


Cheers,
James
Ah, small correction

[code]
std::insert_iterator<PathSet_t>(commonFileSet, commonFileSet.begin()) );
[\code]

change to just commonFileSet.begin(). Same for the other one too..
Oh dear... school boy error! Sorry chaps, don't bother reading further!
Topic archived. No new replies allowed.