Форум программистов
 

Восстановите пароль или Зарегистрируйтесь на форуме, о проблемах и с заказом рекламы пишите сюда - alarforum@yandex.ru, проверяйте папку спам!

Вернуться   Форум программистов > C/C++ программирование > Общие вопросы C/C++
Регистрация

Восстановить пароль
Повторная активизация e-mail

Купить рекламу на форуме - 42 тыс руб за месяц

Ответ
 
Опции темы Поиск в этой теме
Старый 21.08.2009, 23:41   #1
GroundZero
Новичок
Джуниор
 
Регистрация: 21.08.2009
Сообщений: 2
По умолчанию передать вектор структур в функцию

Вот задался целью передать в функцию вектор структур, или хотябы указатель на него, чтобы производить над ним разлиные операции

Код:
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <vector>
#include <resolv.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <iostream>
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>
 
 
using namespace std;
 
struct input_struct     {   
                 unsigned int ats;
                 unsigned int num1;
                   };  
 
void end_of_str_inpfile(vector<input_struct> input_struct_of_file, int *string_counter) {
        input_struct_of_file.resize(*string_counter);
}
 
int main(int argc, char *argv[]) {
        int string_counter = 15;
        vector<input_struct> input_struct_of_file(10);
        end_of_str_inpfile(input_struct_of_file, &string_counter);
        cout << input_struct_of_file.size();
}

после всех наманипуляций (resize) в функции end_of_str_inpfile

размер вектора всеравно остается равен 10и

может я чего-то доконца не понимаю?

Последний раз редактировалось MaTBeu; 23.08.2009 в 05:47.
GroundZero вне форума Ответить с цитированием
Старый 22.08.2009, 00:48   #2
Руслантус
Наркоман самоучка
Форумчанин
 
Аватар для Руслантус
 
Регистрация: 22.07.2007
Сообщений: 276
По умолчанию

Ты передаёш копию вектора, а тебе нужно передать указатель на него.
Можно использовать амперсант & взятия адреса.
void end_of_str_inpfile(vector<input_str uct> &input_struct_of_file, int *string_counter);
#include <мозг.h>
Руслантус вне форума Ответить с цитированием
Старый 22.08.2009, 10:36   #3
GroundZero
Новичок
Джуниор
 
Регистрация: 21.08.2009
Сообщений: 2
По умолчанию

а вызывать саму функцию также?
end_of_str_inpfile(input_struct_of_ file, &string_counter);


или передавать ссылку на вектор?
end_of_str_inpfile(&input_struct_of _ file, &string_counter);
GroundZero вне форума Ответить с цитированием
Старый 22.08.2009, 11:23   #4
pproger
C++ hater
СтарожилДжуниор
 
Аватар для pproger
 
Регистрация: 19.07.2009
Сообщений: 3,333
По умолчанию

Код:
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <vector>
#include <resolv.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <iostream>
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>


using namespace std;

struct input_struct {
	unsigned int ats;
	unsigned int num1;
};

typedef vector<input_struct> myVector;

void end_of_str_inpfile(myVector &m_vector, int *string_counter) {
	m_vector.resize(*string_counter);
}

int main(int argc, char *argv[]) {
	int string_counter = 15;
	myVector input_struct_of_file(10);
	end_of_str_inpfile(input_struct_of_file, &string_counter);
	cout << input_struct_of_file.size();
}
I invented the term Object-Oriented, and I can tell you I did not have C++ in mind. (c)Alan Kay

My other car is cdr.

Q: Whats the object-oriented way to become wealthy?
A: Inheritance
pproger вне форума Ответить с цитированием
Ответ


Купить рекламу на форуме - 42 тыс руб за месяц



Похожие темы
Тема Автор Раздел Ответов Последнее сообщение
Передача массива структур в функцию, conio.h, и графика. jojahti Общие вопросы C/C++ 1 01.08.2009 21:35
Как передать значение переменной из функции в ACCESSе в функцию в EXCELе? musicitsme Microsoft Office Access 1 01.04.2009 10:24
Вектор? Slavik Microsoft Office Excel 0 23.02.2009 10:30