구조체 포인터 (st * ptr)로 인수를 취하는 루프에서 함수를 호출하고이 데이터를 STL 벡터로 push_back하고 내용을 표시해야합니다. 루프. 어떻게하면 돼? 도와주세요.stl 벡터에 구조체 포인터를 삽입하고 내용을 표시하는 방법
struct st
{
int a;
char c;
};
typedef struct st st;
function(st *ptr)
{
vector<st*>myvector;
vector<st*>:: iterator it;
myvector.push_back(ptr);
it=myvector.begin();
cout<<(*it)->a<<(*it)->c<<endl;
}
이 정확히 맞습니까? 나는 실제 출력을 얻지 못하고있다.
코드 -----
void Temperature_sensor::temp_notification()//calling thread in a class------
{
cout<<"Creating thread to read the temperature"<<endl;
pthread_create(&p1,NULL,notifyObserver_1,(void*)(this));
pthread_create(&p2,NULL,notifyObserver_2,(void*)(this));
pthread_join(p1,NULL);
pthread_join(p2,NULL);
}
void* Temperature_sensor::notifyObserver_1(void *data)
{
Temperature_sensor *temp_obj=static_cast<Temperature_sensor *>(data);
(temp_obj)->it=(temp_obj)->observers.begin();
ifstream inputfile("temp.txt");//Reading a text file
while(getline(inputfile,(temp_obj)->line))
{
stringstream linestream((temp_obj)->line);
getline(linestream,(temp_obj)->temperature,':');
getline(linestream,(temp_obj)->temp_type,':');
cout<<(temp_obj)->temperature<<"---"<<(temp_obj)->temp_type<<endl;
stringstream ss((temp_obj)->temperature);
stringstream sb((temp_obj)->temp_type);
sb>>(temp_obj)->c_type;
ss>>(temp_obj)->f_temp;
cout<<"____"<<(temp_obj)->f_temp<<endl;
(temp_obj)->a.temp=(temp_obj)->f_temp;
(temp_obj)->a.type=(temp_obj)->c_type;
cout<<"------------------q"<<(temp_obj)->a.type<<endl;
(*(temp_obj)->it)->update(&(temp_obj)->a);//Calling the function -------
}
input file temp.txt 20:F 30:C 40:c etc
void Temperature_monitor::update(st *p) {}//need to store in a vector------
... 당신이 묘사 한 코드를 정확히 쓰면? 무엇이 효과가 없었습니까? – Quentin
컴파일을 시도 했습니까? 결과? – Klaus
실제 출력물을 얻지 못한다면 무엇을 얻고 있습니까? 또한 C++은 C가 아닙니다. 당신은'typedef' 라인이 필요 없다.그리고 이와 같이 벡터에 포인터를 두는 용도가 있지만 실제로는 벡터 안에 'st'를 직접 넣는 것이 좋습니다. –