arrays
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4

Thread: arrays

  1. #1
    Join Date
    Apr 2017
    Posts
    1

    arrays

    I have been trying to pass an array into a function for two different problems. I am coming from javascript where arrays are super easy. One is a known array of ints, the other I want to grow an array of strings (I really wanted a Javascript-like object for the string array with key value pairs but I don't think C++ has that) as I go along. C++ arrays are super confusing, why are there so many different ones that don't work?!!! Any help would be great!! Thanks!

    Code:
    //1 does not work
    bool test(vector<int, 5> arr) { 
    
    //2 does not work
    bool test2(int arr[]) {
    
    //3 does not work
    bool test3(vector<int> arr) { 
    
      std::cout << "0. size: " << arr.size() << '\n';
      return false;
    }
    
    int main() {
      //1 does not work
     array <int, 5> data = {1,2,4,7,19};  
     isAscOrder(data);
    
      //2  does not work
     int data[] = { 1,2,4,7,19 };
     isAscOrder(data);
    
      //3  does not work
      inAscOrder({1,2,3,4,5});
    }
    
    
    ALSO none of these work...
    //4
    void sub(vector<string>* r1){
    // 5
    void sub(vector<string>& r1){
    // 6 
    void sub(string *&r1){
    
        r1.push_back(text);
        cout << r1.size();<< endl;
    
      }
    
      // for (int i = 0;i< str.size();i++) {
      //   sub(text + str[i], str.erase(0,i) + str.erase(i + 1), r1);
      // }
    }
    
    int main() {
      // 4 does not work with sub's 4
      vector<string> r1;
      // 5 does not work with sub's 5
      vector<string>* r1;
      // 6 does not work with sub's 6
      string *r1 = new string[2];
    
      // calling sub
      sub(r1);
    }
    Last edited by 2kaud; April 4th, 2017 at 04:13 AM. Reason: Added code tags

  2. #2
    Join Date
    Feb 2017
    Posts
    33

    Re: arrays

    Quote Originally Posted by greeter View Post
    why are there so many different ones that don't work?!!! Any help would be great!
    You need to supply include files for the types you are using and you must prefix these types with their namespaces.

    For starters add these includes at the top of your source file:
    Code:
    #include <vector>
    #include <array>
    #include <string>
    #include <iostream>
    All types defined in these include files are in namespace std so you prefix like this,
    Code:
    std::vector<int> v1;
    std::vector<std::string> v2;
    
    std::array<int,5> a1;
    std::array<std::string,10> a2;
    
    std::cout << "size= " << v1.size() << std::endl;
    That should get you going. I know C++ can be frustrating at first but don't give up.

  3. #3
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    5,201

    Re: arrays

    [When posting code, please use code tags. Go Advanced, select the formatted code and click '#']

    If you are only using the std namespace, then rather than prefixing these types with std:: everytime, you can tell the compiler that these types are in the std namespace.

    Code:
    #include <vector>
    #include <array>
    #include <string>
    #include <iostream>
    
    using namespace std;
    ...
    vector<int> v1;
    vector<string> v2;
    
    array<int, 5> a1;
    array<string, 10> a2;
    
    cout << "size= " << v1.size() << endl;
    How are you learning c++? What book are you using.

    You might find this site of interest
    http://www.learncpp.com/

    string array with key value pairs but I don't think C++ has that
    I don't know javascript, but c++ has map that works with key/value pairs. It is an associative container. See http://www.cplusplus.com/reference/map/map/

    Consider
    Code:
    #include <map>
    #include <string>
    #include <iostream>
    using namespace std;
    
    int main()
    {
    	map<string, int> msi;
    
    	msi["qwe"] = 2;
    	msi["asd"] = 3;
    
    	cout << "Value of 'qwe' is " << msi["qwe"] << endl;
    	cout << "Value of 'asd' is " << msi["asd"] << endl;
    
    	cout << endl << "msi contains" << endl;
    
    	for (const auto & m : msi)
    		cout << m.first << " " << m.second << endl;
    }
    Displays
    Code:
    Value of 'qwe' is 2
    Value of 'asd' is 3
    
    msi contains
    asd 3
    qwe 2
    Last edited by 2kaud; April 4th, 2017 at 05:04 AM.
    All advice is offered in good faith only. 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/

    C, C++ Compiler: Microsoft VS2017

  4. #4
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    5,201

    Re: arrays

    I have been trying to pass an array into a function for two different problems. One is a known array of ints, the other I want to grow an array of strings
    If the size of an array is known at compile time, then either a c++ type array can be used, or a c-style array. For passing as a parameter to a function c++ type array is probably easier.

    Consider
    Code:
    #include <array>
    #include <iostream>
    #include <vector>
    #include <string>
    using namespace std;
    
    const int arrsize = 5;
    
    using myArray = array<int, arrsize>;
    using myCarr = int[arrsize];
    using myVect = vector<string>;
    
    void suba(const myArray& arr)
    {
    	for (const auto& a : arr)
    		cout << a << " ";
    
    	cout << endl;
    }
    
    void subc(const myCarr arr)
    {
    	for (int a = 0; a < arrsize; ++a)
    		cout << arr[a] << " ";
    
    	cout << endl;
    }
    
    void subv(const myVect& vec)
    {
    	for (const auto& v : vec)
    		cout << v << " ";
    
    	cout << endl;
    }
    
    int main()
    {
    	myArray mya { 2, 4, 6, 8, 10 };
    	suba(mya);
    
    	myCarr myb { 12, 14, 16, 18, 20 };
    	subc(myb);
    
    	myVect mv{ "qwe", "asd" };
    	subv(mv);
    
    	mv.push_back("zxc");
    	mv.push_back("poi");
    	subv(mv);
    }
    This displays
    Code:
    2 4 6 8 10
    12 14 16 18 20
    qwe asd
    qwe asd zxc poi
    Note that containers are usually passed by reference. As pass by value involves a copy of the container which can expensive, pass by value is usually only used when such a copy is needed. When passing a c-style array, this is actually passed by pointer value (not by container) so passing by value only uses a copy of a pointer. Also note that passing by c-style array the function to which the array is passed is not easy to obtain the size of the passed array like it is when using a c++ array type.
    Last edited by 2kaud; April 4th, 2017 at 06:11 AM.
    All advice is offered in good faith only. 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/

    C, C++ Compiler: Microsoft VS2017

Posting Permissions

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


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This a Codeguru.com survey!


HTML5 Development Center