473,786 Members | 2,445 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

STL Vectors call by reference using vectors of structure

Digital Don
19 New Member
I have a problem sending the Vectors as Referencial parameters..i.e . I am having a struct vector

Expand|Select|Wrap|Line Numbers
  1.  
  2. struct board
  3. {
  4.      int r;
  5.      int c;
  6. };
  7.  
  8. void rd(vector <struct board> cp)
  9. {
  10. cout<<"\nr="<<cp[0].r;
  11. cp[0].r=8;
  12. }
  13. //---------------------
  14. /*This Read function should catch the vector reference and 
  15. any modification done to this cp should affect the cpy in main()*/
  16. //---------------------
  17. void read(vector <struct board> cp)
  18. {
  19. for(int i=0;i<4;i++)
  20. for(int j=0;j<4;j++)
  21. {
  22. struct board x;
  23. x.r=i;
  24. x.c=j;
  25. cp->push_back(x);
  26. rd(*cp);
  27. cout<<"\nr="<<cp[0].r;
  28. //---------------------------------------
  29. /*How do I access the r and c in the cp? I have tried a lot of thinsg but
  30. was unsuccessful in accessing the r and c in cp[0]? */
  31. //---------------------------------------
  32. }
  33. void main()
  34. {
  35.  
  36. vector <struct board> cpy;
  37.  
  38. read(&cpy);
  39.  
  40. for(int i=0;i<4;i++)
  41. //for(int j=0;j<4;j++)
  42. cout<<"\nCpy took "<<cpy[i].r<<" , "<<cpy[i].c<<endl;
  43. //-------------------
  44. Any effects in the functions to cpy by call by 
  45. reference should affect this cpy too...
  46. }
  47.  
I have 2 problems....

1 is call by reference for vector array variables...

2 is accessing the r and c in the cpy struct variable through the pointer?


Please help....

I was actually trying to use the single dimentional array for declaring this i.e.
struct board x[50];

but this said that I was supposed to initialize all the variables as this was not easy I thought of shifting to vectors and now I have this problem...

Thank You in advance...

Regards,
Digital Don...
Mar 8 '07 #1
3 4315
horace1
1,510 Recognized Expert Top Contributor
you should be able to pass vectors by reference using & so
Expand|Select|Wrap|Line Numbers
  1. void read(vector <struct board> &cp)
  2. {
  3. for(int i=0;i<4;i++)
  4. for(int j=0;j<4;j++)
  5. {
  6. struct board x;
  7. x.r=i;
  8. x.c=j;
  9. cp.push_back(x);
  10. rd(cp);
  11. cout<<"\nr="<<cp[0].r;
  12. }
  13.  
and call it
Expand|Select|Wrap|Line Numbers
  1. vector <struct board> cpy;
  2. read(cpy);
  3.  
Mar 8 '07 #2
horace1
1,510 Recognized Expert Top Contributor

I was actually trying to use the single dimentional array for declaring this i.e.
struct board x[50];

Thank You in advance...

Regards,
Digital Don...
not sure what your problem is with an array, e.g.
Expand|Select|Wrap|Line Numbers
  1. void rd(struct board cp[50])
  2. {
  3. cout<<"\nr="<<cp[0].r;
  4. cp[0].r=8;
  5. }
  6. //---------------------
  7. /*This Read function should catch the vector reference and 
  8. any modification done to this cp should affect the cpy in main()*/
  9. //---------------------
  10. void read(struct board cp[50])
  11. {
  12. int index=0;
  13. for(int i=0;i<4;i++)
  14. for(int j=0;j<4;j++)
  15. {
  16. struct board x;
  17. x.r=i;
  18. x.c=j;
  19. cp[index++]=x;
  20. rd(cp);
  21. cout<<"\nr="<<cp[0].r;
  22. //---------------------------------------
  23. /*How do I access the r and c in the cp? I have tried a lot of thinsg but
  24. was unsuccessful in accessing the r and c in cp[0]? */
  25. //---------------------------------------
  26. }
  27. int main()
  28. {
  29.  
  30. struct board cpy[50];
  31.  
  32. read(cpy);
  33.  
  34. for(int i=0;i<4;i++)
  35. //for(int j=0;j<4;j++)
  36. cout<<"\nCpy took "<<cpy[i].r<<" , "<<cpy[i].c<<endl;
  37. //-------------------
  38. //Any effects in the functions to cpy by call by 
  39. //reference should affect this cpy too...
  40. system("pause");
  41. }
  42.  
arrays are passed by reference so you don't need the &
Mar 8 '07 #3
Digital Don
19 New Member
The problem with arrays was that it was asking me to "initialliz e the array" which I declared in Main()...and declaring a 'n' sized array is not a good idea...

Thanks for the help regarding the VECTORS...

not sure what your problem is with an array, e.g.
Expand|Select|Wrap|Line Numbers
  1. void rd(struct board cp[50])
  2. {
  3. cout<<"\nr="<<cp[0].r;
  4. cp[0].r=8;
  5. }
  6. //---------------------
  7. /*This Read function should catch the vector reference and 
  8. any modification done to this cp should affect the cpy in main()*/
  9. //---------------------
  10. void read(struct board cp[50])
  11. {
  12. int index=0;
  13. for(int i=0;i<4;i++)
  14. for(int j=0;j<4;j++)
  15. {
  16. struct board x;
  17. x.r=i;
  18. x.c=j;
  19. cp[index++]=x;
  20. rd(cp);
  21. cout<<"\nr="<<cp[0].r;
  22. //---------------------------------------
  23. /*How do I access the r and c in the cp? I have tried a lot of thinsg but
  24. was unsuccessful in accessing the r and c in cp[0]? */
  25. //---------------------------------------
  26. }
  27. int main()
  28. {
  29.  
  30. struct board cpy[50];
  31.  
  32. read(cpy);
  33.  
  34. for(int i=0;i<4;i++)
  35. //for(int j=0;j<4;j++)
  36. cout<<"\nCpy took "<<cpy[i].r<<" , "<<cpy[i].c<<endl;
  37. //-------------------
  38. //Any effects in the functions to cpy by call by 
  39. //reference should affect this cpy too...
  40. system("pause");
  41. }
  42.  
arrays are passed by reference so you don't need the &
Mar 8 '07 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

10
15840
by: Michael Aramini | last post by:
I need to represent 1D and 2D arrays of numeric or bool types in a C++ program. The sizes of the arrays in my intended application are dynamic in the sense that they are not known at compile time, so I'd like to use an STL container class template such as valarray or vector to represent 1D arrays, and valarrays or vectors of valarrays or vectors to represent 2D arrays. As I said the sizes of the arrays in my intended application are...
9
5945
by: Nancy Keuss | last post by:
Hi, I've created a vector of vectors of ints, and I want to pass it as a parameter to a function. Is this possible, and if so, then what is the syntax like for the function header and function prototype when I need to specify the variable type? Thank you, N.
12
1664
by: Fred Ma | last post by:
Hello, I was looking at Meyers's "Effective STL", item 23 about choosing between vectors and maps (at least that the choice for me). In many cases, using sorted vectors is faster for lookups. The two reasons are (1) that vector elements are smaller, since map elements need at least 3 pointers, and (2) contiguity of vector elements in memory ensure fewer page faults.
9
1650
by: pmatos | last post by:
Hi all, I have a few question regarding standard C++ and the use of vectors. Imagine: void foo(vector<int> x); vector<int> c; vector<int> x; ----------
4
2747
by: foxx | last post by:
I have 2D data structure, modelled as a vector of vectors of ints. I'd like to visit each one of the ints and call a function on them. Is there some smart way of doing this without using a double for loop,? I mean how could I go about creating a new kind of iterator that knows how to transverse all the ints in some sequence; or better still, does STL already have such a feature?
3
2886
by: PolkaHead | last post by:
I was wondering if there's a way to traverse a two-dimensional vector (vector of vectors) with a nested for_each call. The code included traverses the "outer" vector with a for_each, than it relies on the PrintFunctor to traverse the "inner" vector with another for_each. Is it possible to nest the second for_each, so I don't have to include a for_each in my function object. Is it possible to do a: for_each(myVec.begin(), myVec.end(),
1
2456
nabh4u
by: nabh4u | last post by:
Hi, I have a problem referencing to Vectors using pointers i.e. I am not able to use "call by reference" on vector variables. I have a "read()" function in "x.cpp" and "main()" in "y.cpp". I have 3 vector variables in Main(). I want the read function to read the values into the vector using the address I send of the vectors.. Sample code: //x.cpp void read(vector <int> a,vector <int> b,vector < vector <int> > c)
2
2371
by: joraara | last post by:
Hi. I hope someone would be able to help me with this: I need to declare a structure composed by a couple of vectors. But I need those vectors to have an open dimension, I mean non-especific size (for example: short vector) so that the user doesn't have to be tied under a maximun limit. I'm using Visual C++, but the compiler won't let me use such a way of declaring a vector inside the structure (although I can do it provided it's not...
3
2240
by: friendkitty | last post by:
Hi All, I m a new member here.I am now writing a program that render Vertex Normals.I m now trying to implement a data structure that will best suit computing vertex normals.For that computation , my data structure should have accessed to the faces adjacent to each vertices. first i read all vertices ,and faces from .m file into a structure.And stored in vectors. vector<Vertex> vertices; vector<Face> faces; Then i render that model.No...
0
9647
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9492
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
10108
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9960
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7510
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6744
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5532
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4064
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2894
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.