Yoda
Yoda

Reputation: 18068

returning vector<string> in c++

I get: C:\Documents and Settings\ppp\Pulpit\Zadanie3Infix\main.cpp|72|error: conversion from 'std::vector<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::allocator<std::basic_string<char, std::char_traits<char>, std::allocator<char> > > >*' to non-scalar type 'std::vector<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::allocator<std::basic_string<char, std::char_traits<char>, std::allocator<char> > > >' requested|

and i don't know why. The function suppose to return vector of strings and it does, i used all of these * & and it does not work. Please correct me so it will return this vector.

it works like that s = "(2+23)" it will return it will return vector with tokens.

  vector<string> split(string s){
    vector<string>* v = new vector<string>();
    int i = 0; bool b;string el = "";
    while(i < s.size()){

        if(isNotNumber(s.at(i))){
            el = s.at(i);
            v->push_back(el);
            cout<<el<<" to operator"<<endl;
            el = "";
            i++;
        }else{
            while( i <s.size() && !isNotNumber(s.at(i))){//LENIWE WYLICZANIE !!!
                el = el + s.at(i);
                cout<<s.at(i)<<" to liczba"<<endl;
                i++;
            }
            v->push_back(el);
            el = "";
        }
    }
    cout<<"PO while"<<endl;
    for(int i = 0; i < v->size();i++){
        cout<<v->at(i)<<endl;
    }

    return v;
}

and what about that

stack<string>* stack = new stack<string>();

type specifier before stack ;////////////////

Upvotes: 0

Views: 6099

Answers (4)

Phillip Ngan
Phillip Ngan

Reputation: 16106

This is really Blastfurnace's answer, ...

but convert the line:

vector<string>* v = new vector<string>();

into

vector<string> v;

and change all the v-> into v.

This way you fix the memory leak too. (The memory allocated by new vector<string>() is never deleted). However, vector<string> v; puts the memory on the stack and is automatically deleted when it goes out of scope.

Upvotes: 1

Jerry Coffin
Jerry Coffin

Reputation: 490158

The question about whether to return a pointer or a value, or pass in a reference to an existing vector was settled a long time ago: don't do any of the above. Instead, create a generic algorithm that works with iterators:

template <class InIt, class OutIt>
void tokenize(InIt b, InIt e, OutIt o) {
    while (b != e) {
        std::string token;
        while (isdigit(*b))
            token.push_back(*b++);
        if (!token.empty())
           *o++ = token;
        while (!isdigit(*b) && b != e)
            *o++ = std::string(1, *b++);
    }
}

To put the result in a vector of strings, you'd do something like:

std::string i("(2+23)");
std::vector<std::string> tokens;

tokenize(i.begin(), i.end(), std::back_inserter(tokens));

Or, if you wanted to display the tokens, one per line for testing:

tokenize(i.begin(), i.end(), std::ostream_iterator<std::string>(std::cout, "\n"));

...which will produce:

(
2
+
23
)

That fits with what I'd think of as the tokens in this input string. I should add, however, that while it works for that input, it may or may not produce what you expect for other inputs. In particular, it will treat whitespace characters as tokens, which you usually don't want (but does seem to be how the original was intended to work).

Upvotes: 3

mathematician1975
mathematician1975

Reputation: 21351

Your function signature returns a vector, but your return value is a pointer. Change the return value type to pointer to vector or return a vector by value in your function body.

Upvotes: 2

chrisaycock
chrisaycock

Reputation: 37930

Your function split() claims to return vector<string>, however your return value is v. What is v? It's

vector<string>* v

You are attempting to return a pointer but your function declaration states that it is not returning a pointer. Actually, there's no need for a pointer at all in your function; just use a regular vector<string> or pass in a vector by reference:

void split(string s, vector<string>& v){

Upvotes: 4

Related Questions