이중벡터 함수를 만들어 이중벡터 값을 return 할려고


vector<vector<string>> func1() { 

vector<vector<string>> vec2; 

return vec2;

}


와 같이 작성한다면 에러 발생



해결책]

typedef를 이용하여 이중벡터를 타입정의


typedef vector<string> vString;

typedef vector<vString> vvString;


vvString func1(){

vvString vec2;

return vec2;

}


와 같이 표현 시 제대로 동작.

이 때 typedef vector<vector<string>> vvString 하지 않는 이유는

'>>' 문자열을 이상하게 받아들임 --> 따라서 위와 같이 2번 typedef를 사용하여 정의해주어야 됨

Posted by halloRa
,

출처: http://kldp.org/node/59767



#inlcude <cctype> / #include <ctype.h>


isalpha(char); // http://www.cplusplus.com/reference/cctype/isalpha/?kw=isalpha

isdigit(char);  // http://www.cplusplus.com/reference/cctype/isalpha/?kw=isalpha

Posted by halloRa
,

출처: http://stackoverflow.com/questions/5461972/boostfiltering-streambuf-with-gzip-decompressor-how-to-access-line-by-line



ifstream fin_unmapped_read(output.c_str(), ios_base::in | ios_base::binary);

filtering_streambuf<input> in;

in.push(bzip2_decompressor());

in.push(fin_unmapped_read);

//boost::iostreams::copy(in, cout);


std::istream incoming(&in);
getline(incoming, transfer);


Posted by halloRa
,

출처: http://stackoverflow.com/questions/588307/c-obtaining-milliseconds-time-on-linux-clock-doesnt-seem-to-work-properl



#include <sys/time.h>
#include <ctime>
#include <stdio.h>
#include <unistd.h>
int main()
{
   
struct timeval start, end;

   
long mtime, seconds, useconds;    

    gettimeofday
(&start, NULL);
    usleep
(2000);
    gettimeofday
(&end, NULL);

    seconds  
= end.tv_sec  - start.tv_sec;
    useconds
= end.tv_usec - start.tv_usec;

    mtime
= ((seconds) * 1000 + useconds/1000.0) + 0.5;

    printf
("Elapsed time: %ld milliseconds\n", mtime);

   
return 0;
}

Posted by halloRa
,


int n=10;

stringstream ss;
ss << n;
cout << ss.str();        // "10"
ss.str(std::string());
ss.clear();
int k=9;
ss << k;
cout << ss.str();        // "9"



혹은

1. ss.str("");

2. ss.str(std::string());

3. ss.str(""); ss.clear();


와 같이 사용해도 모두 정상 동작.

Posted by halloRa
,

2차원으로 만들고 싶을 경우:


vector<int> col(size, 0);

vector< vector<int> > row(size, col);


-> 이렇게 할 경우 총 (size, size)의 공간이 만들어지고 이 후에는 일반적인 배열을 사용하듯이 접근 가능!

'프로그래밍 > C/C++' 카테고리의 다른 글

프로그램 수행 시 수행 시간 측정  (0) 2012.10.05
stringstream clear  (0) 2012.07.30
Group Sorting  (0) 2012.07.09
이중 if 문에서의 break  (0) 2012.07.05
ofstream 파일 출력시 floating point 고정 시키는 법  (0) 2012.06.22
Posted by halloRa
,

출처: http://stackoverflow.com/questions/1106476/group-sorting-a-vector-in-c


static bool CompareWidget(const Widget& w1, const Widget& w2)
{
   
if(w1.GetGroupNumber() != w2.GetGroupNumber())
       
return (w1.GetGroupNumber() < w2.GetGroupNumber());
   
if(w1.GetHeight() != w2.GetHeight())
       
return (w1.GetHeight() < w2.GetHeight();
   
/// etc
   
return false;
}


 
static void SortWidgetVector(WidgetVector& widgetVector)
 
{
      std
::sort(widgetVector.begin(), widgetVector.end(), CompareWidget);
 
}

'프로그래밍 > C/C++' 카테고리의 다른 글

stringstream clear  (0) 2012.07.30
다중벡터 초기화  (0) 2012.07.11
이중 if 문에서의 break  (0) 2012.07.05
ofstream 파일 출력시 floating point 고정 시키는 법  (0) 2012.06.22
vector ifstream  (0) 2012.05.28
Posted by halloRa
,

출처: http://www.winapi.co.kr/clec/cpp1/4-5-2.htm


결론적으로 이중일 경우 두 번째 if문 안에서 break로 밖으로 나오더라도

첫번째 if문이 해제가 안되므로 따로 설정을 해주어야 한다.

'프로그래밍 > C/C++' 카테고리의 다른 글

다중벡터 초기화  (0) 2012.07.11
Group Sorting  (0) 2012.07.09
ofstream 파일 출력시 floating point 고정 시키는 법  (0) 2012.06.22
vector ifstream  (0) 2012.05.28
TCHAR  (0) 2012.05.09
Posted by halloRa
,


#include <iomanip> 추가해주고

oftstream fout;

double a = 1.2039485;

double b = 14.29349;


fout << setprecision(2) << a << " " << b << endl;

fout << setprecision(2) << fixed << a << " " << b << endl;


-----------------------------------------------------------------------------

output:


1.2 14

1.20 14.29

'프로그래밍 > C/C++' 카테고리의 다른 글

Group Sorting  (0) 2012.07.09
이중 if 문에서의 break  (0) 2012.07.05
vector ifstream  (0) 2012.05.28
TCHAR  (0) 2012.05.09
[MFC] 폴더 지정 대화상자로 선택한 폴더 열기  (0) 2012.05.09
Posted by halloRa
,


출처: http://www.cplusplus.com/forum/beginner/25121/

출처: http://www.daniweb.com/software-development/cpp/threads/143575/an-equivalent-to-a-vectorifstream-member-variable



vector<ifstream*> 과 같이 사용하면 괜춘!


  1. #include <iostream>
  2. #include <string>
  3. #include <vector>
  4. #include <fstream>
  5. using namespace std;
  6. void foo(ifstream& in)
  7. {
  8. }
  9. int main()
  10. {
  11. vector<ifstream*> streams;
  12. for(size_t i = 0; i < 5; i++)
  13. {
  14. ifstream* in = new ifstream;
  15. streams.push_back(in);
  16. streams[i]->open("filename.txt");
  17. }
  18. foo( *streams[1] );
  19. }

Posted by halloRa
,