Convert ArrayVector<string> into ArrayVector<char>

Hello!

So i'm working on a Hangman Game using ArrayVector Data Structure. The goal at the moment that im trying to achieve is to pick a random number from a big text file. Every word is on a different line. Then insert it into an ArrayVector<string> then that word to be turned into characters and be inserted into an ArrayVector<char> that will be used.

Sadly i am unable to find a way to put the ArrayVector<string> inside the ArrayVector<char> and i have no clue what the function i should write exactly should look like. I tried implementing different stuff to use the Copy funcing with my persnal begin() and end() functions, but that was unsucessfull.

Ahelp help is welcome. Thanks in advance!
Any help would be appreciated, thanks !
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
  // Hangman.cpp : This file contains the 'main' function. Program execution begins and ends there.
//

#include "pch.h"
#include <iostream>
#include <string>
#include <iomanip>
#include <algorithm>
#include <cstdlib>
#include <fstream>
#include <random>
using namespace std;



template <typename T>
class ArrayVector {
public:
	ArrayVector();
	int size() const;
	bool empty() const;
	T& operator [](int i);  // Element with index i 
	T& at(int i); // Get element at index i 
	void erase(int i); // Remove a element at index i
	void insert(int i, const T& e); //Insert element at index i 
	void reserve(int N);//Reserve at least N spots
	void print();
private:
	int capacity; //Current array size
	int n; // number of elements in vector;
	T* A; //Array storing the elements
};


int main()
{
	int linecount = 0;
	ArrayVector<char>* l1 = new ArrayVector<char>();
	ArrayVector<string>* l2 = new ArrayVector<string>();
	ifstream file("words.txt");
	string line;
	srand(time(NULL));
	int rd = rand() * rand() % 70000 ;

	while (getline(file, line)) {
		++linecount;
		
		if (linecount == rd) {
			l2->insert(0, line);
		
		}
	}

	l2->print();


}

template <typename T>
ArrayVector<T>::ArrayVector() : capacity(0), n(0), A(NULL) {
}

template <typename T>
int ArrayVector<T>::size() const {
	return n;
}

template <typename T>
bool ArrayVector<T>::empty() const {
	return size() == 0;
}

template <typename T>
T& ArrayVector<T>::operator [](int i) {
	return A[i];
}

template <typename T>
T& ArrayVector<T>::at(int i) {
	return A[i];
}

template <typename T>
void ArrayVector<T>::erase(int i)
{
	for (int j = i + 1; j < n; j++) {
		A[j - 1] = A[j];
	}

	n--;
}

template <typename T>
void ArrayVector<T>::reserve(int N) {
	if (capacity >= N)
		return;
	
	T* B = new T[N];

	for (int j = 0; j < n; j++) {
		B[j] = A[j];
	}


	if (A != NULL) {
		delete [] A;
	}

	A = B;
	capacity = N;

}

template <typename T>
void ArrayVector<T>::insert(int i, const T& e) {
	if (n >= capacity) {
		reserve(max(1, 2 * capacity));
	}

	for(int j = n - 1; j >= i; j--) {
		A[j + 1] = A[j];
	}

	A[i] = e;
	n++;

}

template <typename T>
void ArrayVector<T>::print() {
	int index = 0;
	
	while (index < size()) {
		cout << A[index] << " ";
		index++;
	}

	cout << endl;
}
Last edited on
1
2
//ArrayVector<char>* l1 = new ArrayVector<char>();
ArrayVector<char> l1;
http://www.cplusplus.com/forum/general/138037/

¿why do you use a container if you only want one element?


> I tried implementing different stuff to use the Copy funcing with my persnal
> begin() and end() functions, but that was unsucessfull.
I don't see such code.
(*l2)[0] is a string
(*l2)[0][0] is its first character
(*l2)[0][(*l2).size()-1] is its last character
I didn't show it here since it didn't work and i removed it. I tried many different options. Sorry for not including them.

I am using container beacuse i was doing something else with this beforehand to test stuff out and i was putting more than 1 element. And i just have forgotten to swittch it . Thanks for pointing that out.

Also when i try to implement what you told me in manner of.

copy(l2[0][0], l2[0][(l2->size() - 1)], l1)

I get a bunch of errors regarding xutility, but i might be understanding the " copy " itself wrongly.
Last edited on
I get a bunch of errors regarding xutility

Which errors?

The compiler generally emits useful information when it encounters an error. That information is invaluable, and so you should always include any error messages or warnings you receive, verbatim, in your posts asking for help.
Last edited on
Hello.

Here are the errors i receive after using . Also I am using Visual Studio.

copy(l2[0][0], l2[0][(l2->size() - 1)], l1);

Severity Code Description Project File Line Suppression State
Error C2783 'auto std::_Get_unwrapped_n(const _Iter &,const _Diff)': could not deduce template argument for '__formal' Hangman c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.15.26726\include\xutility 2444


Severity Code Description Project File Line Suppression State
Error C2794 'iterator_category': is not a member of any direct or indirect base class of 'std::iterator_traits<_Iter>' Hangman c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.15.26726\include\xutility 975



Severity Code Description Project File Line Suppression State
Error C2672 '_Get_unwrapped_n': no matching overloaded function found Hangman c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.15.26726\include\xutility 2444


Severity Code Description Project File Line Suppression State
Error C2672 '_Get_unwrapped_n': no matching overloaded function found Hangman c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.15.26726\include\xutility 2444


Severity Code Description Project File Line Suppression State
Error C2938 '_Iter_cat_t<std::string>' : Failed to specialize alias template Hangman c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.15.26726\include\xutility 975


Severity Code Description Project File Line Suppression State
Error C2789 '_UDest': an object of const-qualified type must be initialized Hangman c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.15.26726\include\xutility 2444


Severity Code Description Project File Line Suppression State
Error C3536 '_UDest': cannot be used before it is initialized Hangman c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.15.26726\include\xutility 2445


Severity Code Description Project File Line Suppression State
Error C2062 type 'unknown-type' unexpected Hangman c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.15.26726\include\xutility 975


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#include <iostream>
#include <string>
#include <vector>
#include <cctype>
#include <random>
#include <fstream>

template < typename T > struct ArrayVector {

    // using std::vector for brevity (and robustness)

	int size() const { return seq.size() ; }
	bool empty() const { return seq.empty() ; }
	const T& operator [](int i) const { return seq[i] ; }
	const T& at(int i) const { return seq.at(i) ; }
	void erase( int i) { seq.erase( seq.begin() + i ) ; }
	void insert(int i, const T& e) { seq.insert( seq.begin() + i, e ) ; }
	void reserve(int N) { seq.reserve(N) ; }
	void print() const { for( const T& v : seq ) std::cout << v << ' ' ; }

    private: std::vector<T> seq ;
};

// remove non-alphanumeric characters, convert alpha to lower case
std::string clean( const std::string& str ) {

    std::string result ;
    for( char c : str ) if( std::isalnum(c) ) result += std::tolower(c) ;
    return result ;
}

ArrayVector<std::string> get_words( std::istream& stm ) {

    ArrayVector<std::string> result ;

    std::string word ;
    while( stm >> word ) {

            const auto cleaned_word = clean(word) ;
            if( !cleaned_word.empty() ) result.insert( result.size(), cleaned_word ) ; // push_back
    }

    return result ;
}

std::string random_word( const ArrayVector<std::string>& words ) {

     if( words.empty() ) return {} ;

     static std::mt19937 rng( std::random_device{}() ) ;

     return words[ std::uniform_int_distribution<int>( 0, words.size()-1 )(rng) ] ;

}

ArrayVector<char> to_chars( const std::string& str ) {

    ArrayVector<char> chars ;
    for( char c : str ) chars.insert( chars.size(), c ) ;
    return chars ;
}

int main() {

    const std::string words_file = __FILE__ ; // "words.txt"

    std::ifstream file(words_file) ;
    const auto words = get_words(file) ;
    // words.print() ;
    std::cout << words.size() << " words\n" ;
    
    const auto aword = random_word(words) ;
    std::cout << "picked random word: " << aword << "\nchars are: " ;
    const auto chars = to_chars(aword) ;
    chars.print() ;
}

http://coliru.stacked-crooked.com/a/12423310e799b9ef
Topic archived. No new replies allowed.