• ベストアンサー

boost::formatの値をstring型にコピーしたい

boost::formatの値をstring型にコピーしたいのですが、うまくいきません。boost自体初めてで以下のサイトからダウンロードし、展開後VS2005のVCのインクルードフォルダーにboostフォルダーをまるまるコピーしただけですが・・・ http://sourceforge.net/project/showfiles.php?group_id=7586 boost 1.34.1 #include <iostream> #include <boost/format.hpp> using namespace std; using boost::format; void main(){ double x = 1.234; string str("abc"); //cout << format("%10.3f, [%16s]") % x % str << endl; // サンプルはこうでした。 // いったんstring型に入れて表示させたい。 string y; y = boost::format("%10.3f, [%16s]") % x % str; // エラー箇所 std::cout << y << std::endl; }

  • binma
  • お礼率14% (11/78)

質問者が選んだベストアンサー

  • ベストアンサー
  • jacta
  • ベストアンサー率26% (845/3158)
回答No.2

string y; y = boost::str(boost::format("%10.3f, [%16s]") % x % str); とすればできます。

binma
質問者

補足

ありがとうございます。 boost::str, boost::io::str両方うまくいきました。 しかし、意味不明なワーニング?がたくさん出てきています。 出なくするにはどうしたらよいのでしょうか? >cl /clr test.cpp Microsoft(R) C/C++ Optimizing Compiler Version 14.00.50727.4 for Microsoft(R) .NET Framework Version 2.00.50727.832 Copyright (C) Microsoft Corporation. All rights reserved. test.cpp C:\Program Files\Microsoft Visual Studio 8\VC\INCLUDE\boost/format/alt_sstream_impl.hpp(252) : warning C4996: 'std::char_traits<char>::copy' が古い形式として宣言されました。 C:\Program Files\Microsoft Visual Studio 8\VC\INCLUDE\iosfwd(446) : 'std::char_traits<char>::copy' の宣言を確認してください。 メッセージ: 'You have used a std:: construct that is not safe. See documentation on how to use the Safe Standard C++ Library' C:\Program Files\Microsoft Visual Studio 8\VC\INCLUDE\boost/format/alt_sstream_impl.hpp(223): クラス テンプレート のメンバ関数 'int boost::io::basic_altstringbuf<Ch,Tr,Alloc>::overflow(int)' のコンパイル中 with [ Ch=char, Tr=std::char_traits<char>, Alloc=std::allocator<char> ] C:\Program Files\Microsoft Visual Studio 8\VC\INCLUDE\boost/format/format_class.hpp(136) : コンパイルされたクラスの テンプレート のインスタンス化 'boost::io::basic_altstringbuf<Ch,Tr,Alloc>' の参照を確認してください with [ Ch=char, Tr=std::char_traits<char>, Alloc=std::allocator<char> ] test.cpp(16) : コンパイルされたクラスの テンプレート のインスタンス化 'boost::basic_format<Ch>' の参照を確認してください with [ Ch=char ] Microsoft (R) Incremental Linker Version 8.00.50727.42 Copyright (C) Microsoft Corporation. All rights reserved. /out:test.exe test.obj

その他の回答 (3)

回答No.4

 ちなみに y = (boost::format("%10.3f, [%16s]") % x % str).str();  でもいけますよ。

  • jacta
  • ベストアンサー率26% (845/3158)
回答No.3

> しかし、意味不明なワーニング?がたくさん出てきています。 > 出なくするにはどうしたらよいのでしょうか? 現在、Boostサイトのトップページ http://www.boost.org/ にも掲載されているように、_SCL_SECURE_NO_DEPRECATE マクロの定義が必要です。

  • koko_u_
  • ベストアンサー率18% (459/2509)
回答No.1

boost::io::str を使いたまえ。

関連するQ&A

  • Cygwin boost

    Windows XP sp2 + Cygwin の C++ で正規表現を使いたく setup.exe で boost をインストールし、以下のコードをコンパイル($g++ regextest.cpp) したのですがエラーが出てしまいました。 コード: #include <boost/regex.hpp> #include <iostream> using namespace std; int main () { string str("Free Programming For Windows."); boost::regex reg("Free.+Windows\\."); if (boost::regex_match(str, reg)) { cout << "===> Matched." << endl; } else { cout << "===> Not matched." << endl; } return 0; } エラー: regextest.cpp:1:27: boost/regex.hpp: No such file or directory パスが通っていないようなのですが、どうしたらよいか分かりません。アドバイスいただけたら嬉しいです。 usr/include/boost-1_33_1 に boost/regex.hpp は確認できました。

  • boost::regexで、日本語に利用ついて

    boost_1_47を利用して、置換を行おうとしているのですが、「VIII」を置換しようとするとエラーになります。たぶん「5B」が入っているからかと思うのですが、どのように対処したらよいのでしょうか? #include <boost/regex.hpp> using namespace std; using namespace boost; const boost::regex str("VIII"); よろしくお願いします。

  • stringについて

    C++初心者です。 このプログラムで続行するとエラーがでます。どうしたら無事実行することが出来るのでしょうか? #include<stdio.h> #include <iostream> using namespace std; int main(void) { string str("エラー"); cout << str<< endl; } エラー 1>c:\documents and settings\****\デスクトップ\zisyu12\zisyu12\main.cpp(58) : error C2679: 二項演算子 '<<' : 型 'std::string' の右オペランドを扱う演算子が見つかりません (または変換できません)。 . . . 以下省略

  • C/C++関数間でのStringクラスの扱い

    以下のようなコードを実行してみましたが思い通りに動いてくれません. "sample"という文字列がstrへとコピーされると思ったのですが. stringクラスのc_str()メソッドはconst char*だと言っているので無理矢理キャストしたのが原因でしょうか.stringクラスは記憶領域を自動で変更してくれるのではないのですか.それともこの挙動は仕様ですか. -------- 以下コード -------- #include <iostream> #include <string> using namespace std; int func(char *); int main(void) {     string str("");     func((char *)str.c_str());     cout << "String: " << str << endl;     return EXIT_SUCCESS; } int func(char *buf) {     buf = "sample";     return 0; } -------- 以上コード --------

  • C++文字列の挿入、結合のコードについて

    実行結果のような出力をするためには、 以下のコードの(ウ)(エ)(オ)の部分には何を入れたらよいのでしょうか? よろしくお願いします。 #include <iostream> #include <string> using namespace std; int main( ) { string str1="ABCDEF"; string str2="0123"; string str3; string q; do { (ウ) ; cout << str3 << endl; (エ) ; cout << str1 << endl; cout << "quit?"; cin >> q; } while ( (オ) ); cout << "終了" << endl; return 0; } <実行結果(出力結果)> ABCDEF0123 ABC123DEF quit?q ABC123DEF0123 ABC123123DEF quit?qu ABC123123DEF0123 ABC123123123DEF quit?quit 終了

  • boost::serializationについて

    次のコードがテンプレート辺りでコンパイルできません #include <list> #include <array> #include <fstream> #include <boost/serialization/serialization.hpp> #include <boost/serialization/array.hpp> #include <boost/serialization/list.hpp> #include <boost/archive/binary_iarchive.hpp> #include <boost/archive/binary_oarchive.hpp> std::list<std::array<bool, 3>> lab; std::array<bool, 3> pusharray; pusharray[0] = true; pusharray[1] = false; pusharray[2] = true; lab.push_back(pusharray); lab.push_back(pusharray); std::ofstream ofs("test"); boost::archive::binary_oarchive boa(ofs); boa << lab; ofs.close(); std::ifstream ifs("test"); boost::archive::binary_iarchive bia(ifs); bia >> lab; ifs.close(); エラーメッセージ: c:\program files\microsoft visual studio 10.0\vc\include\boost\archive\basic_binary_iprimitive.hpp(181): warning C4244: '引数' : 'std::streamsize' から 'size_t' への変換です。データが失われる可能性があります。 c:\program files\microsoft visual studio 10.0\vc\include\boost\archive\basic_binary_iprimitive.hpp(152): クラス テンプレート のメンバー関数 'void boost::archive::basic_binary_iprimitive<Archive,Elem,Tr>::load_binary(void *,size_t)' のコンパイル中 with [ Archive=boost::archive::naked_binary_iarchive, Elem=char, Tr=std::char_traits<char> ] c:\program files\microsoft visual studio 10.0\vc\include\boost\archive\detail\iserializer.hpp(362) : コンパイルされたクラスの テンプレート のインスタンス化 'void boost::archive::load_access::load_primitive<Archive,T>(Archive &,T &)' の参照を確認してください with [ Archive=boost::archive::naked_binary_iarchive, T=boost::archive::object_id_type ] (略) そもそもSTLコンテナのネストはserializeできないのでしょうか それともヘッダが足りないのでしょうか

  • clangで例外がキャッチできない?

    初質問です.yahooでも質問したのですがこちらでもしておきます. 趣味でプログラミングをしているものです.clang(コンパイラです)はエラーメッセージが分かりやすいので使っていたのですがg++と挙動が違うようです. 以下のソースのようにC++でboost::lexical_castを使おうとしたのですが,変換に失敗した際に,g++では例外をキャッチしてくれるのにclang(コンパイラです)ではキャッチしてくれず,実効停止してしまうようです.コンパイラオプションの問題でしょうか? ~ ソース test3.cpp ~ #include <iostream> #include <boost/lexical_cast.hpp> #include <string> using namespace std; int main() {     cout << "start" << endl; try { throw 10; // これはキャッチしてくれる } catch (int i) { cout << "thrown:" << i << endl; } cout << "and..." << endl; string str = "not a number"; try { int error = boost::lexical_cast<int>(str); } catch(boost::bad_lexical_cast& e) { // 以下,clangだと全部スルー? cout << "bad_lexical_cast!!" << endl; cout << e.what() << endl; } catch(exception& e) { cout << "exception!!" << endl; cout << e.what() << endl; } catch(...) { cout << "exception!?" << endl; cout << "error" << endl; } cout << "end" << endl; } ~ コンパイル&実行 ~ > clang test3.cpp -lstdc++ -I"C:\Program Files (x86)\boost\boost_1_47" > a.out start thrown:10 and... This application has requested the Runtime to terminate it in an unusual way. Please contact the application's support team for more information. > g++ test3.cpp -I"C:\Program Files (x86)\boost\boost_1_47" > a.exe start thrown:10 and... bad_lexical_cast!! bad lexical cast: source type value could not be interpreted as target end > ~ 環境 ~ OS:Windows 7 clang:version 3.1 g++(mingw):version 4.6.1(あ,でもインストール適当なんで4.7とかとかぶってるかもです) boost:version 1.4.7(1.0っていうんですかね?) 何か激しく勘違いをしている可能性もあるのでお暇な方はどうぞ教えてください. P.S. なんとインデントが消えてしまうのですね!

  • ofstream::getがおかしい!

    ofstream::getの動作がおかしいのです #include <string> #include <iostream> #include <fstream> using namespace std; void main(void) { ifstream ifs; ofstream ofs; string str; char c; str="abc"; cout<<"before str: "<<str<<endl<<endl; ofs.open("gomi");ofs<<str;ofs.close(); ifs.open("gomi");for(str="";ifs.eof()==0;str+=c)ifs.get(c);ifs.close(); cout<<"after str: "<<str<<endl<<endl; } の結果が before str: abc after str: abcc になります 最後のcは何でつくのでしょうか? 回避する方法を教えてください

  • boost::archive::xml_oarchiveについて

    boostのコンパイルが通りません。調べられるだけ調べたのですが、原因として表示されるmake_nvpを利用しても、boost::STATIC_ASSERTION_FAILUREが発生します。わかる方ご教授お願いします。 #define BOOST_LIB_NAME boost_serialization #include <boost/config/auto_link.hpp> #include <boost/serialization/string.hpp> #include <boost/serialization/access.hpp> #include <boost/serialization/nvp.hpp> #include <boost/archive/xml_oarchive.hpp> #include <fstream> class FooData { private: int Data; friend class boost::serialization::access; template <class Archive> void serialization(Archive& ar, const unsigned int version) { ar & boost::serialization::make_nvp("Data",Data) } }; void main(void) { FooData data; //アーカイブ作成 std::ofstream ofs("output.txt"); boost::archive::xml_oarchive oa(ofs); // ファイル書き出し oa << (const FooData&)data; ofs.close(); }

  • 質問です。。。

    いつもすみません。また質問させていただきます。 まずしたのプログラムを見てください・・・ #include <iostream> #include <string> using namespace std; int main() { string name; int tosi; cout << "こんにちは。私はコンピュータです。" <<endl; cout << "あなたの年齢を教えてください" <<endl; cin >> tosi; cout<<name<<"歳なんですか・・・老けてますね(笑)"<<endl; cout << "どこの学校ですか" <<endl; cin >> name; cout<<name<<"That's right!"<<endl; } このプログラムの8行目にあるint tosi;がありますよね。 この単語がなくても string name があったら12行目のプログラムができたのですが、どういうことなのでしょうか? 表現が一部変ですがよろしくお願いします。

専門家に質問してみよう