Like Tree1Likes

Vector initializing first element

This is a discussion on Vector initializing first element within the C++ Programming forums, part of the General Programming Boards category; Lets say I want to initialize the first element of a vector by 5000 bytes. How to do it? Should ...

  1. #1
    Registered User
    Join Date
    Dec 2007
    Posts
    790

    Vector initializing first element

    Lets say I want to initialize the first element of a vector by 5000 bytes.
    How to do it? Should I use strcpy or the likes?

    Code:
    vector<string>big;
    char buf[5000];
    for(int i=0; i<5000;i++)
       buf[i] = '\0';
    
    strcpy(big[0].c_str(), buf);
    
    or
    
    big.push_back(buf);
    Last edited by Ducky; 06-19-2012 at 04:26 AM.
    Compiler MSVC++ 2013 with Code::Blocks.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,660
    You should be able to just write:
    Code:
    vector<string> big(1, string(5000, '\0'));
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Dec 2007
    Posts
    790
    Thanks!
    Compiler MSVC++ 2013 with Code::Blocks.

  4. #4
    Registered User antred's Avatar
    Join Date
    Apr 2012
    Location
    Germany
    Posts
    228
    Quote Originally Posted by laserlight View Post
    You should be able to just write:
    Code:
    vector<string> big(1, string(5000, '\0'));
    This code may be legal, but I dare say it's highly unusual to make a string of 5000 null-termination characters. What good would that do?

  5. #5
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,554
    >>strcpy(big[0].c_str(), buf);
    If your compiler compiles that, then it is broken.
    c_str() returns a const char*, which you may not modify.
    But then again, why do you need to preallocate? std::string is not a buffer.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    Registered User
    Join Date
    Oct 2006
    Posts
    2,370
    Quote Originally Posted by Ducky View Post
    Code:
    vector<string>big;
    char buf[5000];
    for(int i=0; i<5000;i++)
       buf[i] = '\0';
    
    strcpy(big[0].c_str(), buf);
    this will not compile (you'd know that if you tried), and if you take the necessary steps to make it compile (a very bad idea) it will certainly crash your program for the following reasons:

    1. your string vector has not had any space allocated to it yet, and there's no guarantee that the default constuctor allocates a minimum number of elements.
    2. the first argument to strcpy() is a char*. std::string::c_str() returns a const char*. you cannot implicitly convert them, and even if you cast away its const-ness, there is no guarantee that the string has any space allocated to it at all, to say nothing of 5000 characters. it is always a sign of bad design and a serious misunderstanding of concepts if you are using strcpy() to copy into a std::string, or in fact if you are using it at all in a C++ program.

    laserlight's suggestion is probably the best, but as others have said, why do you need a string filled with 5000 null characters?

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,660
    Quote Originally Posted by antred
    This code may be legal, but I dare say it's highly unusual to make a string of 5000 null-termination characters. What good would that do?
    Quote Originally Posted by Elysia
    std::string is not a buffer.
    I presume that this is deliberate use of std::string as a buffer, though I do wonder why exactly this needs to be in a vector.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    Registered User
    Join Date
    Dec 2007
    Posts
    790
    @andred
    @Elysia
    I want to use it instead of a dynamic char* and I will assign it to a char*.
    I will post the function in my other thread (still working on it).
    Remember Elysia you adviced me to use vector instead of char* for dynamic allocation.
    Compiler MSVC++ 2013 with Code::Blocks.

  9. #9
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,554
    Quote Originally Posted by Ducky View Post
    Remember Elysia you adviced me to use vector instead of char* for dynamic allocation.
    Yeah, I can't remember exactly where, but the idea is to use a vector of char (not string):
    std::vector<char>
    This acts like a buffer. You still have to resize it properly before you use it, though (use .resize()).
    antred likes this.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  10. #10
    Registered User
    Join Date
    Dec 2007
    Posts
    790
    I think it doesnt matter in my case if its a string or a char* vector because I cannot use it directly in recv(), I have to reassign it to a char*.
    And use.resize() will resize the number of elements of the vector not the size of one element.
    I only want to use the first element and assign a size to this.
    Compiler MSVC++ 2013 with Code::Blocks.

  11. #11
    Registered User antred's Avatar
    Join Date
    Apr 2012
    Location
    Germany
    Posts
    228
    std::string is not supposed to be used as buffer that you can stick stuff into later. You CAN use it that way if you're careful, but it's unusual and awkward. You really should consider a different approach.

  12. #12
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,554
    You want to use a buffer to receive data. Then, you would do:

    std::vector<char> buf(size_of_buf);
    // Alternatively: buf.resize(size_of_buf);
    foo(&*buf.begin(), buf.size());
    // Alternatively: foo(&buf[0], buf.size());

    buf.begin() will return an iterator. Iterators can be dereferences to get the element they point at. But then you can take their address.
    Since vector is guaranteed to be contiguous, if you get the address of the first element, you have a buffer of .size() elements.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  13. #13
    Registered User
    Join Date
    Dec 2007
    Posts
    790
    So if I understand well if I put a whole file in the char vector, 1 letter will be 1 element of the vector?
    Thats not good for me because I cannot assign that to a char * buf.
    Remember I need to assign the vector to a char* pointer.
    Compiler MSVC++ 2013 with Code::Blocks.

  14. #14
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,554
    We are telling you that you can do that.
    You have a pointer to a buffer.
    A vector is in this case a buffer.
    Therefore, the pointer points to the first element in the vector and you have a pointer to your buffer.
    But don't do that. Use the vector and its interface instead. I showed you how to fill the vector with data already.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  15. #15
    Registered User antred's Avatar
    Join Date
    Apr 2012
    Location
    Germany
    Posts
    228
    Quote Originally Posted by Ducky View Post
    So if I understand well if I put a whole file in the char vector, 1 letter will be 1 element of the vector?
    Thats not good for me because I cannot assign that to a char * buf.
    Remember I need to assign the vector to a char* pointer.
    Code:
    void functionThatTakesCharPtr( char* ptr, std::size_t numberOfElements )
    {
        for ( std::size_t i = 0; i < numberOfElements; ++i )
        {
             ptr[ i ] = .....;
        }
    }
    
    std::vector< char > vec( 5000, '\0' );
    
    functionThatTakesCharPtr( &vec.at( 0 ), vec.size() );

    std::vector was specifically designed to be compatible with old C-style interfaces that fiddle around with arrays and pointers.
    Last edited by antred; 06-19-2012 at 12:24 PM.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. vector<vector<int> > access element.
    By nimitzhunter in forum C++ Programming
    Replies: 0
    Last Post: 01-23-2011, 04:14 AM
  2. vector remove element of element
    By Ducky in forum C++ Programming
    Replies: 6
    Last Post: 09-12-2010, 03:24 PM
  3. Initializing 2D Vector?
    By nacho4d in forum C++ Programming
    Replies: 2
    Last Post: 02-09-2010, 05:11 AM
  4. Insert new element into a vector
    By appointment in forum C++ Programming
    Replies: 7
    Last Post: 08-16-2009, 01:54 AM
  5. max element in vector
    By acosgaya in forum C++ Programming
    Replies: 3
    Last Post: 10-23-2006, 04:07 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21