Thread: convert string of vector<string> s, to char type

  1. #1
    Registered User
    Join Date
    Nov 2019
    Posts
    8

    convert string of vector<string> s, to char type

    How convert string of vector<string> s, to char type as using bits/stdc++.h ?


    Code:
    #include <bits/stdc++.h>
    #include <iostream>
    
     char ch;
    vector<string> s;
    
    int i=0;
    while((ch=s[i++])) {
    
    cout << ch << '--- ' ;
    
    }
    it'll give:

    : error: cannot convert '__gnu_cxx::__alloc_traits<std::allocator<std::__c xx11::basic_
    string<char> >, std::__cxx11::basic_string<char> >::value_type' {aka 'std::__cxx11::basic_string<char>
    '} to 'char' in assignment
    while((ch=s[i++]))
    ^

    How to solve? Thanks
    Last edited by maemec; 11-18-2019 at 07:20 PM.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by maemec
    How convert string of vector<string> s, to char type
    What does it mean to convert a vector<string> to char? For example, suppose you have a vector<string> with two strings, "hello" and "world!". What would the resulting char be, and how did you arrive at that result?

    Quote Originally Posted by maemec
    using bits/stdc++.h ?
    That's another way of saying "using anything from the standard library" since bits/stdc++.h is a non-standard catch-all header for the standard library.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Nov 2019
    Posts
    8
    each string is surely always a char size

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    That's good to know, but you still haven't answered my question, e.g., change it to a vector<string> of two strings, "a" and "b". What would the resulting char be, and how did you arrive at that result?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User Sir Galahad's Avatar
    Join Date
    Nov 2016
    Location
    The Round Table
    Posts
    277
    Quote Originally Posted by maemec View Post
    How convert string of vector<string> s, to char type as using bits/stdc++.h ?


    Code:
    #include <bits/stdc++.h>
    #include <iostream>
    
     char ch;
    vector<string> s;
    
    int i=0;
    while((ch=s[i++])) {
    
    cout << ch << '--- ' ;
    
    }
    it'll give:

    How to solve?
    Think about it this way. A vector<string> an array of array of chars. So what you're really asking is similar to wanting to convert an entire spreadsheet into a single cell! So unless that two-dimensional grid only contains one lone value then it just doesn't make much sense. But if it did it would be as simple as v[r][c] where `v` is the vector, `r` is the row coordinate of the character and `c` the column.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 11
    Last Post: 06-16-2011, 11:59 AM
  2. Replies: 2
    Last Post: 09-12-2010, 09:15 AM
  3. how to convert char string to user defined data type
    By prdeepss in forum C Programming
    Replies: 5
    Last Post: 08-02-2009, 09:59 AM
  4. convert char** (c string array) to std::string[]
    By umen242 in forum C++ Programming
    Replies: 2
    Last Post: 11-11-2008, 05:52 AM
  5. How to convert type string to char/double ?
    By Hermitsky in forum C++ Programming
    Replies: 6
    Last Post: 07-08-2004, 03:25 PM

Tags for this Thread