Pregunta

I have function that has three integer values, representing year, month and day. I want to return these values with dashes '-' in between, as in common date format.

e.g.:

int main() {
    cout << myfunction() << endl; // this should display like this 2014-04-15
}

string myfunction() {
    int year = 2014;
    int month = 04;
    int day = 15;
    // I want to return this value like this
    return year-month-day;//2014-04-15
}

Can someone help me?

¿Fue útil?

Solución 2

In C++03 you can use std::ostringstream class to format a string using operator<< :

#include <string>
#include <sstream>
#include <iomanip>

std::string myfunction() {
    int year=2014;
    int month=04;
    int day=15;

    std::ostringstream oss;
    oss << year << "-" << std::setw(2) << std::setfill('0') 
                                                     << month << "-" << day;
    return oss.str();
           ^^^^^^^^^ // this will yield a formatted string that oss contains
}

In C++11 you can use std::to_string and operator+ to concatenate strings:

#include <string>

std::string myfunction() {
    int year=2014;
    int month=04;
    int day=15;

    std::string s = to_string( year) 
                    + "-" + to_string(month)
                    + "-" + to_string(day);
    return s;
}

however to_string doesn't offer formatting option. If you want to specify format, again, you should look into string stream.

Otros consejos

#include <sstream>
#include <iomanip>    

...

std::stringstream ss;
ss << std::setfill('0') << std::setw(4) << year << "-"
   << std::setw(2) << month << "-" << std::setw(2) << day;
return ss.str();

also, if you don't have C++11, look at boost::lexical_cast(...), or if you don't want boost, look at stringstream.

stringstream ss;
ss << first << "-" << second << "-" << third << endl;
return ss.str();

that have to work.

ostringstream oss;
oss << year << '-' << month << '-' << day;

return oss.str();

You can use a string stream.

string myfunction(){
    int year=2014;
    int month=04;
    int day=15;
    stringstream ss;
    ss << year << "-" << month << "-" << day;
    return ss.str();
}

Don't forget to include sstream header.

If you do not want to use STL, you can also resort to C-style function _itoa. Here is a link to the documentation:

http://msdn.microsoft.com/en-us/library/yakksftt.aspx

But I do suggest you to use stringstream like others are suggesting.

Even in C++ code, I like printf-like format specifiers (in this case, to be used with sprintf()).

You may want to consider this compilable code:

#include <stdio.h>
#include <iostream>
#include <string>

std::string ToString(int year, int month, int day) {
    char buffer[11];
    // YYYY-MM-DD
    // 1234567890
    //          10 + NUL --> 11 characters in buffer

    sprintf(buffer, "%04d-%02d-%02d", year, month, day);
    // sprintf_s on MSVC would be better (and it implicitly gets the dest buffer size).

    return buffer;
}

int main() {
    std::cout << ToString(2014, 4, 15) << std::endl;
}

Output is 2014-04-15, as requested.


PS
If you want to use C++ string streams (from <sstream>), then you should pay attention to padding with a leading 0 the months and days having one digit. For that purpose, consider this StackOverflow question and related answers. The key point is to use setfill('0') and setw(2) for padding.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top