int main (int argc, char * const argv[]) { bool use_non_characters = false; // cout << "use non-characters?:"; // char c[2]; // cin >> c; // if (c[0] == 'y') use_non_characters = true; char filename[] = "words.txt"; // vector<string> lines; MyVector<string> lines; cout << "reading words from " << filename << endl; ifstream data_in(filename, ios_base::in); while(!data_in.eof()){ string tmp_line; getline(data_in,tmp_line); //lines.push_back(tmp_line); lines.add(tmp_line); } data_in.close(); cout << "readed words: " << endl; // for (vector<string>::iterator it=lines.begin(); it < lines.end(); it++) { for (MyVector<string>::iterator it=lines.begin(); it < lines.end(); it++) { cout << *it << endl; if (contains_non_characters(*it) && !use_non_characters) { cout << "contains non characters... aborting program" << endl; return 0; } } cout << endl; cout << "input words(to exit type enter):" << endl; string line; while (getline(cin,line) && line != "exit"){ // cout << "contains non-characters : " << contains_non_characters(line) << endl; if (!use_non_characters && contains_non_characters(line)) { cout << "containst non-characters, ignoring word" << endl; continue; } for (MyVector<string>::iterator it=lines.begin(); it < lines.end(); it++) { if (line.compare(*it) < 0){ lines.insert(*it, line); break; } } } cout << "words input ended" << endl; cout << "result list: " << endl; for (MyVector<string>::iterator it=lines.begin(); it < lines.end(); it++) cout << *it << endl; //записывам список обратно в файл // ofstream data_out(filename, ios_base::out | ios_base::trunc); // for (vector<string>::iterator it=lines.begin(); it < lines.end(); it++) // data_out << *it << endl; // data_out.close(); return 0; }
__declspec(dllexport) int client(char * sharedMemName, char * properVectorName) { //Child process //Open the managed segment managed_shared_memory segment(open_only, sharedMemName); //Find the vector using the c-string name MyVector *myvector = segment.find<MyVector>("MyVector").first; //Use vector in reverse order //std::sort(myvector->rbegin(), myvector->rend()); for (auto it = myvector->begin(); it != myvector->end(); ++it) { //printf("got:%d\n", *it); printf("got:%s\n", (*it).c_str()); } //When done, destroy the vector from the segment return 0; }
MyVector<int> IntersectLists(const MyVector<int> &A, const MyVector<int> &B) { MyVector<int> Res; MyVector<int>::const_iterator IA = A.begin(), IB = B.begin(); while ((IA != A.end()) && (IB != B.end())) if (*IA < *IB) IA++; else if (*IB < *IA) IB++; else { Res.push_back(*IA); IA++; IB++; } return Res; }
int main(int argc, char * * argv) { typedef std::vector<std::string> MyVector; MyVector myVector; myVector.push_back("foo"); myVector.push_back("bar"); myVector.push_back("baz"); std::for_each(myVector.begin(), myVector.end(), [](const std::string & s) { std::cout << s << std::endl; }); return 0; }
void testVectorWithTypedef() { // To avoid some of the cumbersome syntax, many programmers use typedefs // to streamline things. So, repeating the previous example, we end up with: typedef std::vector<int> MyVector; MyVector myVector; myVector.push_back(100); myVector.push_back(200); for (MyVector::const_iterator it = myVector.begin(); it != myVector.end(); it++) { std::cout << *it << "\n"; } }
int main () { //Remove shared memory on construction and destruction struct shm_remove { shm_remove() { shared_memory_object::remove("MySharedMemory"); } ~shm_remove(){ shared_memory_object::remove("MySharedMemory"); } } remover; managed_shared_memory segment( create_only, "MySharedMemory", //segment name 65536); //segment size in bytes //Construct shared memory vector MyVector *myvector = segment.construct<MyVector>("MyVector") (IntAllocator(segment.get_segment_manager())); //Fill vector myvector->reserve(100); for(int i = 0; i < 100; ++i){ myvector->push_back(i); } //Create the vectorstream. To create the internal shared memory //basic_string we need to pass the shared memory allocator as //a constructor argument MyVectorStream myvectorstream(CharAllocator(segment.get_segment_manager())); //Reserve the internal string myvectorstream.reserve(100*5); //Write all vector elements as text in the internal string //Data will be directly written in shared memory, because //internal string's allocator is a shared memory allocator for(std::size_t i = 0, max = myvector->size(); i < max; ++i){ myvectorstream << (*myvector)[i] << std::endl; } //Auxiliary vector to compare original data MyVector *myvector2 = segment.construct<MyVector>("MyVector2") (IntAllocator(segment.get_segment_manager())); //Avoid reallocations myvector2->reserve(100); //Extract all values from the internal //string directly to a shared memory vector. std::istream_iterator<int> it(myvectorstream), itend; std::copy(it, itend, std::back_inserter(*myvector2)); //Compare vectors assert(std::equal(myvector->begin(), myvector->end(), myvector2->begin())); //Create a copy of the internal string MyString stringcopy (myvectorstream.vector()); //Now we create a new empty shared memory string... MyString *mystring = segment.construct<MyString>("MyString") (CharAllocator(segment.get_segment_manager())); //...and we swap vectorstream's internal string //with the new one: after this statement mystring //will be the owner of the formatted data. //No reallocations, no data copies myvectorstream.swap_vector(*mystring); //Let's compare both strings assert(stringcopy == *mystring); //Done, destroy and delete vectors and string from the segment segment.destroy_ptr(myvector2); segment.destroy_ptr(myvector); segment.destroy_ptr(mystring); return 0; }