display vector without looping it

Jul 15, 2013 at 8:57pm
I was looking to make a way to view the contents of a vector without looping the vector? Kind of like in Python, you can just print the list and display its contents without looping it.

I know i can just print the elements in the split function but i wanted to make the split function just return the vector

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <iostream>
#include <string>
#include <sstream>
#include <vector>
using namespace std ;

vector<string> split(string s, char splitter){
    vector<string> temp;
    istringstream iss(s);
    string s2;
    while ( getline( iss, s2, splitter ) ) {
        temp.push_back(s2.c_str());
    }
    return temp;
}


int main(){
    string s = "test this now meich";
    vector<string> v;
    v = split(s, ' ');
    
    for (int i=0; i<v.size(); i++){
        cout << v[i] << endl;
    }
}

Jul 15, 2013 at 9:06pm
No, there is no standard way to do what you are asking. However you could write your own function to do this or overload the stream operator to work for vectors.
Jul 15, 2013 at 9:18pm
Either way you have to iterate through the vector. You could either do as Zhuge said and overload the stream operator, or just write a print method that does this.
Jul 15, 2013 at 9:40pm
well i guess i could just make a function to just loop it and print it as a test function to show me what is in it. Which the purpose is to save me the time of rewriting the loop for every vector self test. I was jsut hoping there would be a builtin or library that would do this instead of creating your own.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#include <iostream>
#include <string>
#include <sstream>
#include <vector>
using namespace std ;

vector<string> split(string s, char splitter='\n'){
    vector<string> temp;
    istringstream iss(s);
    string s2;
    while ( getline( iss, s2, splitter ) ) {
        temp.push_back(s2.c_str());
    }
    return temp;
}

string vector_content(vector<string> v){
    string s;
    s += '[';
    for (int i=0; i<v.size(); i++){
        s += "\"";
        s += v[i] ;
        s += "\"";
        if (v[i] != v.back())
            s += " ,";
    }
    s += ']';
    return s;
}


int main(){
    string s = "some:random text for split method tester";
    vector<string> v;
    cout << vector_content(split(s, ' ')) << endl;
    cout << vector_content(split(s, ':')) << endl;

}


and the python-like:
1
2
3
4
5
6
7
8
9
10
metulburr@ubuntu:~$ python3
Python 3.3.1 (default, Apr 17 2013, 22:30:32) 
[GCC 4.7.3] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> s = "some : random text for split method tester"
>>> s.split()
['some', ':', 'random', 'text', 'for', 'split', 'method', 'tester']
>>> s.split(':')
['some ', ' random text for split method tester']
>>> 
Last edited on Jul 15, 2013 at 9:51pm
Topic archived. No new replies allowed.