如何在C ++中连接char *? 我尝试使用strcat,但这只适用于char *和字符串。 strcpy没有给出所需的输出,因为我认为它会覆盖第二个数组中第一个数组中的值。 我需要在C ++中做这样的事情 (这就是我在java中所做的)
seat=seat+tempGuest.toString()+". " //Java
strcat_s(p, 150,seating[i][j].toString())
;
这会产生错误,因为seating[i][j].tostring()
返回char *类型。
这是我的功能代码
char* Auditorium::toString(){
char* p = new char[150];
cout << "The current seating is\n";
cout << "-------------------------\n";
for (int i = 0; i < rowNum1; i++)
{
for (int j = 0; j < columnNum1; j++)
{
strcat_s(p, 150,seating[i][j].toString());
strcat_s(p, 150," ");
}
strcat_s(p, 150,"\n");
}
return p;
}
seating[i][j].toString()
从以下函数中获取值
char* Guest::toString()
{
char * p = new char[30];
p[0] = firstName[0];
p[1] = '.';
p[2]=lastName[0];
p[3] = '.';
p[4] = '\0';
return p;
}
答案 0 :(得分:3)
在C ++中,您使用std::string
。在这种情况下,您甚至可以使用字符串流:
#include <sstream>
std::string Auditorium::toString() {
std::ostringstream oss;
oss << "The current seating is\n";
oss << "-------------------------\n";
for (int i = 0; i < rowNum1; i++) {
for (int j = 0; j < columnNum1; j++) {
oss << seating[i][j].toString();
}
oss << "\n";
}
return oss.str();
}
没有流,请考虑:
std::string Auditorium::toString() {
std::string result;
for (int i = 0; i < rowNum1; i++) {
for (int j = 0; j < columnNum1; j++) {
result += seating[i][j].toString();
result += ' ';
}
result += '\n';
}
return result;
}
更多的C ++风格,不提供toString
,但提供operator<<
:
<强> Live On Coliru 强>
#include <ostream>
#include <string>
#include <array>
struct Guest {
std::string firstName, lastName;
friend std::ostream& operator<<(std::ostream& os, Guest const& g) {
return os << g.firstName[0] << '.' << g.lastName[0] << '.';
}
};
namespace {
using Row = std::array<Guest, 12>;
inline std::ostream& operator<<(std::ostream& os, Row const& row) {
for (Guest const& guest : row)
os << guest << " ";
return os;
}
}
struct Auditorium {
std::array<Row, 10> seating;
friend std::ostream& operator<<(std::ostream& os, Auditorium const& auditorium) {
for (Row const& row : auditorium.seating)
os << row << "\n";
return os;
}
};
#include <iostream>
int main() {
Auditorium auditorium {
Row {Guest {"Jacob","Lettsom"}, {"Brion","Peasee"}, {"Kelvin","Tomasoni"}, {"Marabel","Fere"}, {"Hertha","Bartlam"}, {"Devondra","Grahl"}, {"Ike","Annott"}, {"Patrick","Cheyenne"}, {"Eada","Dawe"}, {"Marget","Glashby"}, {"Henderson","Glaum"}, {"Cassie","Winsom"}},
Row {Guest {"Pierson","Kitchen"}, {"Elisha","Pass"}, {"Shirline","Wigin"}, {"Lucas","Stanmore"}, {"Norri","Hitzschke"}, {"Mercedes","Blackboro"}, {"Nappie","Breche"}, {"Freda","Mitton"}, {"Neils","Adamou"}, {"Mack","Rannells"}, {"Gaspard","Christoffersen"}, {"Sherwin","Kenwell"}},
Row {Guest {"Wallas","Chellam"}, {"Doralin","Corthes"}, {"Sebastien","Scoble"}, {"Domini","Sprott"}, {"Lazaro","Bunton"}, {"Hinda","Korn"}, {"Renata","Vogt"}, {"Delmar","MacDwyer"}, {"Violette","Villiers"}, {"Nicko","McDirmid"}, {"Susanetta","MacCaughey"}, {"Hillary","McNuff"}},
Row {Guest {"Alex","Downie"}, {"Caro","Westrey"}, {"Burr","Kalkofer"}, {"Ruy","Shelmerdine"}, {"Winfield","Beri"}, {"Isacco","Ellwell"}, {"Gideon","Beaford"}, {"Simon","Blaylock"}, {"Willy","Kloser"}, {"Guillemette","Boult"}, {"Mariya","Oehme"}, {"Emory","Angless"}},
Row {Guest {"Jammie","Klimek"}, {"Monro","Passman"}, {"Page","Kornes"}, {"Giorgio","Couttes"}, {"Alexine","Glayzer"}, {"Jeni","Ferschke"}, {"Rock","Farrants"}, {"Katrinka","Schnieder"}, {"Irina","Ault"}, {"Antonetta","Griss"}, {"Pammy","Bertenshaw"}, {"Erinna","Terbeck"}},
Row {Guest {"Evania","Sympson"}, {"Randy","Colvin"}, {"Legra","Osinin"}, {"Michaeline","Moroney"}, {"Adiana","Westbrook"}, {"Kellsie","Smeeton"}, {"Rodrick","Russ"}, {"Winny","Eggleston"}, {"Odille","Jerosch"}, {"Stacee","Liepina"}, {"Lucila","Jedras"}, {"Selma","Perch"}},
Row {Guest {"Lynda","Palmar"}, {"Gracie","Jennick"}, {"Cleveland","Mordue"}, {"Elias","Manssuer"}, {"Kimbra","Cicchitello"}, {"Genni","Woliter"}, {"Gale","Baudassi"}, {"Johann","Swindlehurst"}, {"Andrei","De" "Laspee"}, {"Edythe","Dorr"}, {"Janela","Fydo"}, {"Silvia","Suerz"}},
Row {Guest {"Rachel","Shoutt"}, {"Germain","Strangward"}, {"Margot","Son"}, {"Electra","Wookey"}, {"Samuel","Tight"}, {"Brande","Cable"}, {"Ford","Aitken"}, {"Noll","Woolf"}, {"Jourdan","Morewood"}, {"Milo","Trimming"}, {"Adair","Peck"}, {"Darnall","Loftus"}},
Row {Guest {"Dene","Creer"}, {"Leena","Autry"}, {"Dickie","Wiggington"}, {"Josephine","Eagan"}, {"Hetty","Yoodall"}, {"Gayla","Dibson"}, {"Elwood","Torri"}, {"Eunice","Scapelhorn"}, {"Everett","Bedingfield"}, {"Ralf","Lodeke"}, {"Steve","Dockreay"}, {"Rod","Bruford"}},
Row {Guest {"Siouxie","Rayment"}, {"Sheila-kathryn","Neal"}, {"Land","Beggi"}, {"Carson","Ferriman"}, {"Aile","Nias"}, {"Paige","Juckes"}, {"Cazzie","Leadstone"}, {"Selinda","Parlet"}, {"Rayner","Kesby"}, {"Murvyn","Laming"}, {"Lorianna","Pardey"}, {"Joane","Sneesby"}},
};
std::cout << auditorium << "\n";
}
打印
J.L. B.P. K.T. M.F. H.B. D.G. I.A. P.C. E.D. M.G. H.G. C.W.
P.K. E.P. S.W. L.S. N.H. M.B. N.B. F.M. N.A. M.R. G.C. S.K.
W.C. D.C. S.S. D.S. L.B. H.K. R.V. D.M. V.V. N.M. S.M. H.M.
A.D. C.W. B.K. R.S. W.B. I.E. G.B. S.B. W.K. G.B. M.O. E.A.
J.K. M.P. P.K. G.C. A.G. J.F. R.F. K.S. I.A. A.G. P.B. E.T.
E.S. R.C. L.O. M.M. A.W. K.S. R.R. W.E. O.J. S.L. L.J. S.P.
L.P. G.J. C.M. E.M. K.C. G.W. G.B. J.S. A.D. E.D. J.F. S.S.
R.S. G.S. M.S. E.W. S.T. B.C. F.A. N.W. J.M. M.T. A.P. D.L.
D.C. L.A. D.W. J.E. H.Y. G.D. E.T. E.S. E.B. R.L. S.D. R.B.
S.R. S.N. L.B. C.F. A.N. P.J. C.L. S.P. R.K. M.L. L.P. J.S.
答案 1 :(得分:0)