CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 2 of 2 FirstFirst 12
Results 16 to 18 of 18
  1. #16
    2kaud's Avatar
    2kaud is online now Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,824

    Re: vector of pointers to a vector of objects, pointer or iterator?

    It looks like this needs something like a double de-reference to access the original object values, (*subset_row)->c_distance. Is that correct?
    In the loop it is an iterator so needs a de-reference to get the value - which is an iterator which needs a deference to get the actual data.

    Am I right that you can't just use (*data_subset[i]) as if it were an iterator?
    data_subset[i] is an iterator - so *(data_subset[i]) gives the underlying data. However, you'd normally use -> as

    Code:
    	for (size_t e = 0; e < data_subset.size(); ++e)
    		cout << data_subset[e]->row_name << " " << data_subset[e]->c_distance << endl;
    Maybe something like,
    or maybe

    Code:
    #include <vector>
    #include <string>
    #include <algorithm>
    #include <iostream>
    
    using namespace std;
    
    class row_data {
    public:
    
    	// class constructor
    	row_data() : set_A(false), set_B(false), id(0), c_distance(0.0), row_name("") { }
    
    	// member variables
    	bool set_A, set_B;
    	unsigned int id;
    	double c_distance;
    	std::string row_name;
    	std::vector<double> double_data;
    };
    
    typedef std::vector<row_data>::const_iterator row_data_vec_it;
    typedef std::vector<row_data_vec_it>::const_iterator veccit;
    
    bool sort_vec_it_sm2lg_c_distance(row_data_vec_it a, row_data_vec_it b) { return a->c_distance < b->c_distance; }
    
    int main()
    {
    	vector<row_data> data_rows;
    
    	// populate data_rows
    
    	// data subset container
    	vector<row_data_vec_it> data_subset;
    
    	for (row_data_vec_it location = data_rows.begin(); location != data_rows.end(); ++location)
    		if (location->set_B) { data_subset.push_back(location); }
    
    	// after data_subset has been populated, sort on c_distance
    	sort(data_subset.begin(), data_subset.end(), sort_vec_it_sm2lg_c_distance);
    
    	// now we have a sorted subset of the original data
    	for (veccit point_B = data_subset.begin(); point_B != data_subset.end() - 2; ++point_B)
    		for (veccit point_A = point_B + 1; point_A < data_subset.end() - 1; ++point_A)
    			for (veccit point_D = point_A + 1; point_D < data_subset.end(); ++point_D) {
    
    				// print some stuff
    				cout << "current set of three points is" << endl;
    				cout << "point_B: " << (*point_B)->row_name << endl;
    				cout << "point_A: " << (*point_A)->row_name << endl;
    				cout << "point_D: " << (*point_D)->row_name << endl;
    
    				// pass to a function
    			}
    }
    Last edited by 2kaud; August 20th, 2018 at 02:16 PM.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  2. #17
    2kaud's Avatar
    2kaud is online now Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,824

    Re: vector of pointers to a vector of objects, pointer or iterator?

    you have declared a second kind if iterator to iterate over the vector of iterators
    If you moved from c++98 to the latest c++17 standard, you wouldn't need to define iterator types at all! Consider

    Code:
    #include <vector>
    #include <string>
    #include <algorithm>
    #include <iostream>
    
    using namespace std;
    
    class row_data {
    public:
    	bool set_A = false, set_B = false;
    	unsigned int id = 0;
    	double c_distance = 0.0;
    	std::string row_name;
    	std::vector<double> double_data;
    };
    
    int main()
    {
    	vector<row_data> data_rows;
    
    	// populate data_rows
    
    	// data subset container
    	vector<decltype(data_rows.cbegin())> data_subset;
    
    	for (auto location = data_rows.cbegin(); location != data_rows.cend(); ++location)
    		if (location->set_B)
    			data_subset.push_back(location);
    
    	// after data_subset has been populated, sort on c_distance
    	sort(data_subset.begin(), data_subset.end(), [](const auto& a, const auto& b) {return a->c_distance < b->c_distance;});
    
    	// now we have a sorted subset of the original data
    	for (auto point_B = data_subset.cbegin(); point_B < data_subset.cend() - 2; ++point_B)
    		for (auto point_A = point_B + 1; point_A < data_subset.cend() - 1; ++point_A)
    			for (auto point_D = point_A + 1; point_D < data_subset.cend(); ++point_D) {
    
    				// print some stuff
    				cout << "current set of three points is" << endl;
    				cout << "point_B: " << (*point_B)->row_name << endl;
    				cout << "point_A: " << (*point_A)->row_name << endl;
    				cout << "point_D: " << (*point_D)->row_name << endl;
    
    				// pass to a function
    			}
    }
    The only 'explicit' type that is used is vector<row_data>!
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  3. #18
    Join Date
    Sep 2018
    Posts
    9

    Re: vector of pointers to a vector of objects, pointer or iterator?

    I have same issue but still can't resolve. If you found any best solution please let me know.

Page 2 of 2 FirstFirst 12

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured