PDA

View Full Version : Several questions about the collection.



alex009988
07-28-2019, 07:08 AM
Hello, I want to rewrire c++ program that uses vectors to vba.
The first questions, is there a way to show all items of collection with Debug.print without it to be converted to array? Join(a, " ") dosent work.


Sub vector()
Dim c As New Collection, a()
c.Add "1"
c.Add "2"
c.Add "3"
ReDim a(1 To c.Count)
For i = 1 To c.Count
a(i) = c(i)
Next i
Debug.Print Join(a, " ")
End Sub

Is there a more effective way to set elements to collection the same like to array a=Array(1,2,3).
I also ask some questions later on about collections.
Regards Alex,

Bob Phillips
07-28-2019, 07:14 AM
For Each cmember In c

Debug.Print cmember
Next cmember

alex009988
07-28-2019, 10:18 AM
Well Thank you. I want to perform that c++ code in excel


#include <vector>
#include <iostream>
#include <iterator>
using namespace std;


void recurse(vector<int>& a, int pos, int remaining) {
if (remaining == 0) {
ostream_iterator<int> i(cout, " ");
copy(a.begin(), a.end(), i);
cout << "\n";
return; }
if (pos == a.size()) { return; }
for (int i = remaining; i >= 0; i=i-1) {
a[pos] = i;
recurse(a, pos + 1, remaining - i);
}
}


int main() {
int v=3 , k, z;
k=v;
z = v + 1;
vector<int> a(k);
for (v = -1; v < z; v=v+1) {
recurse(a, 0, v);
}
return 0;
}

And I took a part of it for now to test it whether it works or not.


#include <vector>
#include <iostream>
#include <iterator>
using namespace std;


void recurse(vector<int>& a, int pos, int remaining) {
if (remaining == 0) {
ostream_iterator<int> i(cout, " ");
copy(a.begin(), a.end(), i);
cout << "\n";
return; }
if (pos == a.size()) { return; }
for (int i = remaining; i >= 0; i=i-1) {
a[pos] = i;
// recurse(a, pos + 1, remaining - i);
}
}


int main() {
int v=3 , k, z;
k=v;
z = v + 1;
vector<int> a(k);
recurse(a, 0, 0);
return 0;
}

That shot give "0 0 0" output
Here is c++ online compiler. https://www.onlinegdb.com/online_c++_compiler
So I've tried to make the same stuff on excel


Function recurse(a As Collection, pos As Integer, remaining As Integer)
If remaining = 0 Then
For Each cmember In a
Debug.Print cmember
Next cmember
End If
If pos = a.Count Then Exit Function
For i = remaing To 0
a(pos) = i
Next i
End Function
Sub AllParts2()
Dim v As Integer, k As Integer, z As Integer, result As String, a As New Collection
k = v
z = v + 1
result = recurse(a, 0, 0)
End Sub

And I get nothing. Maybe it's because that Debug.print is inside the function or...?

Bob Phillips
07-28-2019, 12:10 PM
That shot give "0 0 0" output
Here is c++ online compiler. https://www.onlinegdb.com/online_c++_compiler
So I've tried to make the same stuff on excel


Function recurse(a As Collection, pos As Integer, remaining As Integer)
If remaining = 0 Then
For Each cmember In a
Debug.Print cmember
Next cmember
End If
If pos = a.Count Then Exit Function
For i = remaing To 0
a(pos) = i
Next i
End Function
Sub AllParts2()
Dim v As Integer, k As Integer, z As Integer, result As String, a As New Collection
k = v
z = v + 1
result = recurse(a, 0, 0)
End Sub

And I get nothing. Maybe it's because that Debug.print is inside the function or...?

I can't help you with the c++, but I can see in the VBA that the argument is called remaining, but your code refers to remaing. You should prefix the module with Option Explicit.