Thread: character array and vector

  1. #1
    Registered User
    Join Date
    Jan 2009
    Posts
    197

    character array and vector

    how do i store character array in vector
    Code:
    #include<iostream>
    #include<queue>
    #include<vector>
    #include<string>
    using namespace std;
    int main()
    {
        char *s;
    vector</*how do i define*/>v;
    vector</*how do i define*/>::iterator it;
    
        
        char alts[10];
        for(int i=0;i<5;i++)
        {
        scanf("%s",alts);
       v.push_back(alts);
    }
    for ( it=myvector.begin() ; it < myvector.end(); it++ )
        cout << " " << *it;
    
     
       
        return 0;
    }

  2. #2
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    How about vector< char >? Yes, it really is that simple! Just keep in mind that push_back stores a single element. To concatenate 'alts' to 'myvector', you'll need to use the 'insert' member function. Oh, and you really shouldn't be mixing scanf with C++ streams (and anyway, they can do all of that and more). Also, another option is to just use an std::string - you can pass it to std::getline, and concatenate with the += operator, etc...

  3. #3
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Try replacing /* how do i define*/ with string.

    Even better, try using cin (the C++ stream that allows reading from standard input) rather than scanf(). There are specific operations that allow C++ strings to be read from C++ istreams, and no danger of overflowing arrays of char.

    Also, in the loop to output the vector, the end condition will often be brtter expressed as "it != myvector.end()"
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  4. #4
    Registered User
    Join Date
    Jan 2009
    Posts
    197
    i would like to store an array of chars
    eg:
    ABC
    DEF

    Moreover my requirement is to use scanf

  5. #5
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    To convert an array of char to vector of char:

    Code:
    char array[ N ] = { ... };
    std::vector< char > vec( array, array + N );
    If reading from a stream, then...

    Code:
    #include <iterator>
    
    std::cin >> std::noskipws;
    std::vector< char > vec( std::istream_iterator< char >( std::cin ), std::istream_iterator< char >() );
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  6. #6
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by dpp View Post
    i would like to store an array of chars
    eg:
    ABC
    DEF

    Moreover my requirement is to use scanf
    Seems like a completely contrived/artificial requirement to me.

    In any event, the first line of my previous post will meet your original requirement. A std::string contains a representation of an array of chars.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Resource Management question..
    By Raigne in forum C++ Programming
    Replies: 37
    Last Post: 03-08-2008, 09:36 AM
  2. efficiency issues with downsizing vector or array
    By robatino in forum C++ Programming
    Replies: 5
    Last Post: 12-29-2006, 11:09 AM
  3. Array trying to be a vector
    By Chaplin27 in forum C++ Programming
    Replies: 18
    Last Post: 03-03-2005, 09:48 PM
  4. Vector & char array problems. :-(
    By rjcarmo in forum C++ Programming
    Replies: 2
    Last Post: 05-19-2003, 08:24 PM
  5. my vector class..easy dynamic arrays
    By nextus in forum C++ Programming
    Replies: 5
    Last Post: 02-03-2003, 10:14 AM